Used in the Python Jupyter Notebook achieve the following edge detection code, filtering the image, the process to achieve image processing out certain functions may be a bit rough. Install on opencv libraries can refer to: Python installation under opencv libraries and some of the issues summarized .
First, edge extraction
Import CV2 # Import opencv library
# read original gradation image
Image cv2.imread = ( " test.bmp " )
cv2.imshow ( " Image " , Image) # original image named "image" is displayed
# threshold image division processing, image processing is about to black and white binary image
RET, image1 = cv2.threshold (image, 80, 255, cv2.THRESH_BINARY) # binary (black and white binary), the representative RET threshold, low threshold is 80, 255 is a high threshold
cv2.imshow ( ' image1 ' , image1) # picture after the thresholding process name "image1" is displayed
# binary image color-inverted, the inverted image pixel
height, width = image1.shape # return to image size
image2 = image1.copy ()
for I in Range (height):
for J in Range (width):
image2 [I, J] = (255- image1 [I, J])
cv2.imshow ( ' image2 ' , image2) # The reaction color after treatment image named "image2" displayed
# edge extraction using Canny function
image2_3 = cv2.Canny (image2,80,255) # set 80 to a low threshold, a high threshold 255
cv2.imshow ( ' image2_3 ' , image2_3) # edge extraction after the image is named "image2_3" is displayed
# again color-inverted image of the edge extraction is black, the remainder is white, the same method image2
height1, width1 = image2_3.shape
Image3 = image2_3.copy ()
for I in Range (height1):
for J in Range (width1):
Image3 [I, J] = (255- image2_3 [I, J])
cv2.imshow ( ' Image3 ' , Image3) # after the inverse color edge extraction picture named "image3" displayed
cv2.waitKey (0) # wait for keyboard input, do not enter an infinite waiting
cv2.destroyAllWindows () # destroy all windows
operation result:
Second, the image filter
Import CV2
Import numpy
Import matplotlib.pyplot AS plt
from PIL Import Image, ImageDraw, ImageFont
# to add Chinese characters to the picture
DEF ImgText_CN (img, text, left, Top, textColor = (0, 255, 0), textSize = 20 ):
iF (isinstance (img, numpy.ndarray)): # determine whether the OpenCV image type
img = Image.fromarray (cv2.cvtColor (img, cv2.COLOR_BGR2RGB))
Draw = ImageDraw.Draw (img)
fontText = ImageFont. TrueType ( " font / simhei.ttf " , the textSize, encoding = " UTF-. 8 ")
Draw.text ((left, Top), text, textColor, font = fontText)
return cv2.cvtColor (numpy.asarray (img), cv2.COLOR_RGB2BGR)
# read the original gray-scale images
src = cv2.imread ( " test2 .bmp " )
cv2.imshow ( " the src " , the src) # original image named" the src "shown
src_cn = ImgText_CN (the src, " original image " , 5, 5, (255, 0, 0), 20 )
cv2.imshow ( " src_cn " , src_cn) # adding text src named "src_cn" is displayed and
# mean filter
des cv2.blur = (src, (5, 5 ))
cv2.imshow ( "des " , des) # The mean filtering process after the src named" des "and displayed
des_cn = ImgText_CN (des, " mean filter " ,. 5,. 5, (255, 0, 0), 20 is )
cv2.imshow ( " des_cn " , des_cn) # adding text des named "des_cn" and displayed
# median filtering
Med = cv2.medianBlur (the src,. 5 )
cv2.imshow ( " Med " , Med) # the median filter after src named "med" and displayed
med_cn = ImgText_CN (Med, " median filtering " ,. 5,. 5, (255, 0, 0), 20 is )
cv2.imshow ( " med_cn " , med_cn) #Adding text med named "med_cn" and displayed
# Gaussian
Gauss = cv2.GaussianBlur (src, (5, 5 ), 0)
cv2.imshow ( " Gauss " , Gauss) # after the Gaussian filter processing src named "gauss" and displayed
gauss_cn = ImgText_CN (gauss, " Gaussian " ,. 5,. 5, (255, 0, 0), 20 is )
cv2.imshow ( " gauss_cn " , gauss_cn) # adding text gauss named is "gauss_cn" and displayed
# Gaussian edge detection
gaussedge = cv2.Canny (gauss, 0,50 )
cv2.imshow ( " gaussedge " , gaussedge) # the gauss rear edge detection processing named "gaussedge" and displayed
= ImgText_CN gaussedge_cn (gaussedge, " Gaussian edge detection " ,. 5,. 5, (0, 255, 0), 20 is )
cv2.imshow ( " gaussedge_cn " , gaussedge_cn) # adding text gaussedge named "gaussedge_cn" and displayed
cv2.waitKey (0)
operation result: