Welcome to DOFBOT SE repository
7.Picture cut
7.Picture cut
Image cutting first reads the image, and then obtains the pixel area in the array. In the following code, select the shape area X: 300-500 Y: 500-700. Note that the image size is 800*800, so the selected area should not exceed this resolution.
Code path:/home/yahboom/Dofbot/4.opencv/02_OpenCV Transform/02Picture clipping.ipynb
ximport cv2img = cv2.imread('yahboom.jpg', 1)dst = img[500:700,300:500] #Here select the rectangular area X:300-500 Y:500-700#cv2.imshow('image',dst)
#cv2.waitKey(0)The following will display two compressed image comparison displays in the jupyterLab control.
xxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])Below are the before and after comparison images:
xxxxxxxxxximport ipywidgets.widgets as widgetsimage_widget1 = widgets.Image(format='jpg', )image_widget2 = widgets.Image(format='jpg', )# display the container in this cell's outputdisplay(image_widget1)display(image_widget2)img1 = cv2.imread('yahboom.jpg',1)
image_widget1.value = bgr8_to_jpeg(img1) #original imageimage_widget2.value = bgr8_to_jpeg(dst) #cut imageAfter the program is finished running, you can see that some parts have been cut out.
