python + opencv Note (2) - the edge of the filling

python + opencv edge of the filling

First, the edge of the filling

I believe that many small partners like playing computer, encountered such a situation: sometimes the wallpaper for the computer when the original a good look at the complete picture, replaced by a computer wallpaper is incomplete or duplicate images composed by a number of wallpaper . In fact, here there appears filled.

Edge padding: Since an operation for image convolution, edge pixels generally can not handle the most, so not the center of the convolution kernel most edge pixel. This requires first boundary image is filled, then the convolution operation depending on the filling algorithm, a new image is obtained after filling image.

Second, edge padding function prototypes

函数原型:cv2.copyMakeBorder(src,top, bottom, left, right ,borderType,value)

src: The image needs to be filled

Filling the length of the boundary of the image above: top

bottom: the length of the boundary of the image is filled below

left: the length of the image fill the left border

right: length of filled right border image

borderType: type of border

BORDER_REPLICATE : Copy method, that is the most edge pixel replication. For example: aaaa | abcdefg | ggggg

BORDER_REFLECT : reflection method that is most edge pixel axis of symmetry. For example: fedcba | abcdefg | gfedec

BORDER_REFLECT_101 : reflection method, is most edge of the pixel axis of symmetry, but BORDER_REFLECT differentiated. For example: fedcb | abcdefg | fedec

BORDER_WRAP : packaging method, i.e. left and right borders of the image are connected, is connected to the upper and lower boundaries. For example: cdefgh | abcdefgh | abcdefg

BORDER_CONSTANT : Constant law.

If there still do not understand, we can follow the code below and compare renderings, to understand the role of each method.

value: filling border color, normally used to fill the constant process.

Third, the interpretation and implementation code

1, reads the code image and the display image

1  # libraries required 
2  Import CV2 AS CV
 . 3  Import matplotlib.pyplot AS PLT
 . 4  Import numpy AS NP
 . 5  
. 6  # reading image cv2.imread ( "image path") 
. 7 IMG = cv.imread ( ' lufei.jpg ' )
 . 8  
. 9  # display image functions 
10  DEF cv_show (name, IMG):
 . 11      cv.imshow (name, IMG)
 12 is      cv.waitKey (0)
 13 is      cv.destroyAllWindows ()

Read about an image, display and other basic operations, specific view a blog on the cat.

2, the core code

 1 #各个边界需要填充的值
 2 top_size,bottom_size,left_size,right_size=(50,50,50,50)
 3 #复制法
 4 replicate=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_REPLICATE)
 5 #反射法
 6 reflect=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,cv.BORDER_REFLECT)
 7 reflect101=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,cv.BORDER_REFLECT_101)
 8 #外包装法
 9 wrap=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,cv.BORDER_WRAP)
10 # Constant method, often filled with values 
. 11 Constant = cv.copyMakeBorder (IMG, top_size, bottom_size, left_size, right_size, cv.BORDER_CONSTANT, value = (0,255,0))
. 1 IMG1 = img.copy ()
 2  # added to the respective images corresponding to the text 
. 3 cv.putText (IMG1, " Original " , (0,50), cv.FONT_HERSHEY_COMPLEX, 2, (0,250,0), 4,8 )
 . 4 cv.putText (replicate, " replicate " , (0,50), cv.FONT_HERSHEY_COMPLEX, 2, (0,250,0), 4,8 )
 . 5 cv.putText (the reflect, " the reflect " , (0,50) , cv.FONT_HERSHEY_COMPLEX, 2, (0,250,0), 4,8 )
 . 6 cv.putText (reflect101, " reflect101 " , (0,50), cv.FONT_HERSHEY_COMPLEX, 2, (0,250,0), 4,8 )
 7 cv.putText (wrap,"wrap",(0,50),cv.FONT_HERSHEY_COMPLEX,2,(0,250,0),4,8)
 8 cv.putText(constant,"constant",(0,50),cv.FONT_HERSHEY_COMPLEX,2,(0,250,0),4,8)
 9 
10 #显示图像
11 cv_show("original",img1)
12 cv_show("replicate",replicate)
13 cv_show("reflect",reflect)
14 cv_show("reflect101",reflect101)
15 cv_show("wrap",wrap)
16 cv_show("constant",constant)

3, FIG contrast effect

In order to fill the images before and after contrast, bloggers uploaded a screenshot here, no geometric scaling, full of original size.

The renderings can clearly be seen that the size of the original and after filling is not the same as FIG.

4, experience sharing details

 (1) In the cat coding on edge fill process, using a window corresponding to each image is a method of displaying an image, so that the code written, can not be directly compared quickly the differences between each image.

In order to quickly compare the difference image obtained in each method, may be used np.vstack () or np.hstack () compared with the image in a window.

1 rec=np.hstack((replicate,reflect))
2 cv_show("replicate_reflect",rec)

 

 note:

Question A: Using np.vstack () or np.hstack () function, the image must be the same size, or will be error.

Solve A: Without knowing the size of the two images, you can use shape to see.

E.g:

 

B. Issues use np.vstack () or np.hstack () function, the displayed image may appear incomplete cases.

Solution B: This is because the window size is set according to the size of the screen, if the height or length of the window exceeds the size of the computer screen, which will be displayed in a computer the length or height.

E.g:

 

(2) can be used to display all the image contrast canvas

Code:

1 plt.subplot(231),plt.imshow(img,"gray"),plt.title("original")
2 plt.subplot(232),plt.imshow(replicate,"gray"),plt.title("replicate")
3 plt.subplot(233),plt.imshow(reflect,"gray"),plt.title("reflect")
4 plt.subplot(234),plt.imshow(reflect101,"gray"),plt.title("reflect101")
5 plt.subplot(235),plt.imshow(wrap,"gray"),plt.title("wrap")
6 plt.subplot(236),plt.imshow(constant,"gray"),plt.title("constant")
7 plt.show()

 

 效果图:

 

 

Guess you like

Origin www.cnblogs.com/cyt99/p/12457384.html