Skip to content

Commit

Permalink
Programs related to convolution are added
Browse files Browse the repository at this point in the history
* .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
TravorLZH committed Aug 17, 2019
1 parent c79640b commit 9eb0596
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
*.face
15 changes: 15 additions & 0 deletions conv.py
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)
37 changes: 37 additions & 0 deletions edge_detect.py
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)
26 changes: 26 additions & 0 deletions invert.py
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)
Binary file added lena.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions sketch.py
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)
4 changes: 0 additions & 4 deletions track.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@

tracker=None

# Used by mouse_handler() and tracking
flag=False
drawing=False

# Stores current frame
frame=None

Expand Down

0 comments on commit 9eb0596

Please sign in to comment.