-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Programs related to convolution are added
* .gitignore: Ignore .DS_Store file generated by Mac's Finder * conv.py: Created. It convolves a picture and show the comparison between the old picture and the convolved one. * edge_detect.py: Using sobel operator, this program is able to detect all the edges of a picture * invert.py: A simple program that inverts the color of a picture * lena.png: Universal sample picture for image recognition * sketch.py: This program both detects the edges in the pictures and invert the colors of the result in order to make it look like a sketch of this picture * track.py: Remove useless variables
- Loading branch information
Showing
7 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.DS_Store | ||
*.face |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python3 | ||
import cv2 | ||
import numpy as np | ||
|
||
img=cv2.imread("lena.png",cv2.IMREAD_COLOR) | ||
kernel=np.array([ | ||
[-1,0,1], | ||
[-2,0,2], | ||
[-1,0,1] | ||
],np.float32) | ||
print(kernel) | ||
result=cv2.filter2D(img,-1,kernel) | ||
merged=np.hstack([img,result]) | ||
cv2.imshow("Lena",merged) | ||
cv2.waitKey(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env python3 | ||
import cv2 | ||
import numpy as np | ||
import sys | ||
import os | ||
|
||
argc=len(sys.argv) | ||
if argc<2: | ||
print("usage: %s image_file" % sys.argv[0]) | ||
exit(1) | ||
|
||
if os.path.exists(sys.argv[1])==False: | ||
print("%s does not exist" % sys.argv[1]) | ||
exit(-1) | ||
|
||
# Output file name | ||
output_name="%s_sobel.png" % os.path.splitext(sys.argv[1])[0] | ||
|
||
# Detect edges using sobel operator | ||
sobel_x=np.array([ | ||
[-1,0,1], | ||
[-2,0,2], | ||
[-1,0,1] | ||
]) | ||
|
||
sobel_y=np.array([ | ||
[1,2,1], | ||
[0,0,0], | ||
[-1,-2,-1] | ||
]) | ||
|
||
img=cv2.imread(sys.argv[1],cv2.IMREAD_GRAYSCALE) | ||
result_x=cv2.filter2D(img,-1,sobel_x) | ||
result_y=cv2.filter2D(img,-1,sobel_y) | ||
result=result_x+result_y | ||
|
||
cv2.imwrite(output_name,result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
|
||
argc=len(sys.argv) | ||
if argc<2: | ||
print("usage: %s image_file" % sys.argv[0]) | ||
exit(1) | ||
|
||
import os | ||
if os.path.exists(sys.argv[1])==False: | ||
print("%s does not exist" % sys.argv[1]) | ||
exit(-1) | ||
|
||
out_name="%s_inverted.png" % os.path.splitext(sys.argv[1])[0] | ||
|
||
import cv2 | ||
import numpy as np | ||
|
||
img=cv2.imread(sys.argv[1],cv2.IMREAD_COLOR) | ||
img2=np.zeros_like(img) | ||
|
||
for i,row in enumerate(img): | ||
for j,col in enumerate(row): | ||
img2[i,j]=0xFF-col | ||
|
||
cv2.imwrite(out_name,img2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Default high contrast level | ||
level=5 | ||
|
||
if __name__=="__main__": | ||
import sys | ||
|
||
argc=len(sys.argv) | ||
if argc<2: | ||
print("usage: %s image_file" % sys.argv[0]) | ||
exit(1) | ||
|
||
import os | ||
|
||
if os.path.exists(sys.argv[1])==False: | ||
print("%s does not exist" % sys.argv[1]) | ||
exit(-1) | ||
|
||
out_name="%s_sketch.png" % os.path.splitext(sys.argv[1])[0] | ||
|
||
import numpy as np | ||
import cv2 | ||
|
||
sobel_x=np.array([ | ||
[-1,0,1], | ||
[-2,0,2], | ||
[-1,0,1] | ||
]) | ||
|
||
sobel_y=np.array([ | ||
[1,2,1], | ||
[0,0,0], | ||
[-1,-2,-1] | ||
]) | ||
|
||
# This function helps make higher contrast of colors | ||
# NOTE: The variable x must be normalized previously! | ||
def f(x): | ||
return 1/(1+np.exp(-level*(x-0.5))) | ||
|
||
def make_sketch(img): | ||
result_x=cv2.filter2D(img,-1,sobel_x) | ||
result=cv2.filter2D(img,-1,sobel_y) | ||
result+=result_x | ||
# Now invert the image | ||
proceeded=np.zeros_like(result) | ||
for i,row in enumerate(result): | ||
for j,col in enumerate(row): | ||
normalized=(0xFF-col)/0xFF | ||
proceeded[i][j]=int(f(normalized)*0xFF) | ||
return proceeded | ||
|
||
if __name__=="__main__": | ||
img=cv2.imread(sys.argv[1],cv2.IMREAD_GRAYSCALE) | ||
final=make_sketch(img) | ||
# Now save the output | ||
cv2.imwrite(out_name,final) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters