A series of studying basic image processing from scratch (4).
Refer to the OpenCV-Python tutorial Image recognition book https://www.amazon.co.jp/dp/4061529129/ It is a policy to promote understanding of the processing that is being done in.
Python 3.7.0 OpenCV 4.1.0 Jupyter Notebook
Perform contour extraction on the binarized panda image. The return value contours of findContours is the coordinate set for each contour, and the hierarchy is the hierarchical structure of the contour. This time, the outline is made into two layers. The outline was drawn on the original panda image.
rinkaku.py
import cv2
import numpy as np
from matplotlib import pyplot as plt
from pylab import rcParams #Change the size of the image display
%matplotlib inline
rcParams['figure.figsize'] = 25, 20 #Image display size
img = cv2.imread('/brabra/6.jpg',0)
#Compress the image
img = cv2.resize(img, dsize=None, fx=0.15, fy=0.15)
#Binarization(The threshold is from the binarization of Otsu)
ret,thresh = cv2.threshold(img,59,255,0)
#Invert
thresh= cv2.bitwise_not(thresh)
#Contour extraction(RETR_CCOMP has two layers)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #Convert to RGB format
#Draw the found outline on the original image
for i in range(len(contours)):
#Draw the top layer (green)
if hierarchy[0][i][3] == -1:
cv2.drawContours(img, contours, i, (0, 255, 0), 2)
#Draw the second layer (light blue)
else:
cv2.drawContours(img, contours, i, (0, 255, 255), 2)
plt.imshow(img)
It is not perfect, but the outline is mostly extracted.
The contour is drawn with many vertices when attention is paid to details, but when the vertices up to that point are not needed, the contour can be approximated. Here, I tried to approximate the contour of the foot part.
rinkakukinzi.py
img = cv2.imread('/brabra/6.jpg',0)
img = cv2.resize(img, dsize=None, fx=0.15, fy=0.15)
ret,thresh = cv2.threshold(img,59,255,0)
thresh= cv2.bitwise_not(thresh)
#Contour extraction
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #Convert to RGB format
#Outline of the foot part
cnt = contours[7]
#Contour approximation epsilon is the degree of approximation
epsilon = 0.01*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
#Draw an approximate contour on the original image
cv2.drawContours(img, [approx], -1, (0, 255, 0), 2)
plt.imshow(img)
It was nice and angular.
It is possible to draw a figure that circumscribes the contour. I drew quadrilaterals, circles, ellipses that circumscribe, and similar lines.
gaisetsu.py
img = cv2.imread('C:/brabra/6.jpg',0)
img = cv2.resize(img, dsize=None, fx=0.15, fy=0.15)
ret,thresh = cv2.threshold(img,59,255,0)
thresh= cv2.bitwise_not(thresh)
#Contour extraction
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #Convert to RGB format
#Draw the outline of the foot
cv2.drawContours(img, contours, 7, (0, 255, 0), 2)
cnt = contours[7]
#External rectangle
x,y,w,h = cv2.boundingRect(cnt)
img0 = img.copy()
img0 = cv2.rectangle(img0,(x,y),(x+w,y+h),(0,255,0),2)
#External rectangle considering rotation
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
img00 = img.copy()
img00 = cv2.drawContours(img00,[box],0,(0,0,255),2)
#Minimum circumscribed circle
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
img1 = img.copy()
img1 = cv2.circle(img1,center,radius,(0,255,255),2)
#Ellipse fitting
ellipse = cv2.fitEllipse(cnt)
img2 = img.copy()
img2 = cv2.ellipse(img2,ellipse,(255,255,0),2)
#Straight line fitting
rows,cols = img.shape[:2]
[vx,vy,x,y] = cv2.fitLine(cnt, cv2.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
img3 = img.copy()
img3 = cv2.line(img3,(cols-1,righty),(0,lefty),(255,255,255),2)
plt.subplot(231),plt.imshow(img0)
plt.title('en'), plt.xticks([]), plt.yticks([])
plt.subplot(232),plt.imshow(img00)
plt.title('en'), plt.xticks([]), plt.yticks([])
plt.subplot(233),plt.imshow(img1)
plt.title('en'), plt.xticks([]), plt.yticks([])
plt.subplot(234),plt.imshow(img2)
plt.title('daen'), plt.xticks([]), plt.yticks([])
plt.subplot(235),plt.imshow(img3)
plt.title('line'), plt.xticks([]), plt.yticks([])
plt.show()
I deepened my understanding of contour extraction.
--Image processing using OpenCV (OpenCV-Python tutorial) http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_table_of_contents_imgproc/py_table_of_contents_imgproc.html#py-table-of-content-imgproc
--Image recognition (machine learning professional series) https://www.amazon.co.jp/dp/4061529129/
Recommended Posts