I detected and extracted the face here, but I thought that I could realize it by copying without thinking too much. This time, I wanted to try to detect objects other than the face, so I tried to imitate it even though I didn't have much knowledge.
I made a face classifier for Dir en gray using TensorFlow-④ Face extraction
opencv tutorial challenge 9 outline (area) in OpenCV
Have a look at this.
I made a face classifier for Dir en gray using TensorFlow --② Environment construction
This is Apo.
The following three are detected.
The following three rectangular images are output.
contour_detect.py
#!/usr/local/bin/python
#! -*- coding: utf-8 -*-
import cv2
import numpy as np
#Specified image(path)Detects the object of and outputs the image of the circumscribing rectangle
def detect_contour(path):
#Load image
src = cv2.imread(path, cv2.IMREAD_COLOR)
#Convert to grayscale image
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
#Binarization
retval, bw = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
#Extract contour
# contours : [region][Point No][0][x=0, y=1]
# cv2.CHAIN_APPROX_NONE:Hold the midpoint
# cv2.CHAIN_APPROX_SIMPLE:Do not hold midpoint
contours, hierarchy = cv2.findContours(bw, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
#Rectangle detected number (default is 0)
detect_count = 0
#Processing for each contour
for i in range(0, len(contours)):
#Calculate contour area
area = cv2.contourArea(contours[i])
#Exclude noise (too small area) and overall contour (too large area)
if area < 1e2 or 1e5 < area:
continue
#External rectangle
if len(contours[i]) > 0:
rect = contours[i]
x, y, w, h = cv2.boundingRect(rect)
cv2.rectangle(src, (x, y), (x + w, y + h), (0, 255, 0), 2)
#Save image for each circumscribing rectangle
cv2.imwrite('{File Path}' + str(detect_count) + '.jpg', src[y:y + h, x:x + w])
detect_count = detect_count + 1
#Display circumscribed rectangular image
cv2.imshow('output', src)
cv2.waitKey(0)
#End processing
cv2.destroyAllWindows()
if __name__ == '__main__':
detect_contour('{File Path}/{file name}.jpg')
--Read the rectangle as "Kukei" instead of "Tankei". --If there are too many objects, the limit of extraction will come out (?). ――Isn't it possible to erase the overlapping contour lines when cutting?
--Investigate how to erase the outline. ――Check the calculation formula to improve the detection accuracy a little more. ――Let's learn an object by applying TensorFlow.
-I tried object detection using Python and OpenCV -I tried to sort out objects from the image of steak set meal-① Object detection -I tried to sort out the objects from the image of the steak set meal-② Overlap number sorting -I tried to sort out the objects from the image of the steak set meal-③ Similar image heat map detection -I tried to sort out the objects from the image of the steak set meal-④ Clustering -I tried to sort out objects from the image of steak set meal-⑤ Similar image feature point detection edition
Recommended Posts