How do i crop a contourn?
Hello. I'm trying to write a code that identifies the eyes of parakeets. Currently, i'm using a code that identifies circles within a certain threshold and it is working great. The problem: I need the result image colored. I tried using masks to crop the contourn on the clone img with no luck. Any ideas on what i can do?
Those are the result images:
This is the original image:
This is the kind of result that i want
import cv2
import numpy as np
import imutils
def nothing(x):
pass
# Load an image
img = cv2.imread('papagaio.png')
# Resize The image
if img.shape[1] > 600:
img = imutils.resize(img, width=600)
# Create a window
cv2.namedWindow('Treshed')
# create trackbars for treshold change
cv2.createTrackbar('Treshold','Treshed',0,255,nothing)
while(1):
# Clone original image to not overlap drawings
clone = img.copy()
# Convert to gray
gray = cv2.cvtColor(clone, cv2.COLOR_BGR2GRAY)
# get current positions of four trackbars
r = cv2.getTrackbarPos('Treshold','Treshed')
# Thresholding the gray image
ret,gray_threshed = cv2.threshold(gray,r,255,cv2.THRESH_BINARY)
# Blur an image
bilateral_filtered_image = cv2.bilateralFilter(gray_threshed, 5, 175, 175)
# Detect edges
edge_detected_image = cv2.Canny(bilateral_filtered_image, 75, 200)
# Find contours
contours, _= cv2.findContours(edge_detected_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_list = []
for contour in contours:
# approximte for circles
approx = cv2.approxPolyDP(contour,0.01*cv2.arcLength(contour,True),True)
area = cv2.contourArea(contour)
if ((len(approx) > 8) & (area > 30) ):
contour_list.append(contour)
# Draw contours on the original image
cv2.drawContours(clone, contour_list, -1, (255,0,0), 2)
# there is an outer boundary and inner boundary for each eadge, so contours double
print('Number of found circles: {}'.format(int(len(contour_list)/2)))
#Displaying the results
cv2.imshow('Objects Detected', clone)
cv2.imshow("Treshed", gray_threshed)
# ESC to break
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# close all open windows
cv2.destroyAllWindows()
could you post the original image (papagaio.png) here
Already did!
Sorry I don't understand what you want. Can you give result image (handly make) ?
Updated the post with a example of what i want. Thank you for your attention!
@idervas, you already have a loop there, that evaluates the contourArea.
find the contour with the largest area, then crop using
cv2.getBoundingRect(largest_contour)
I'm sorry, but i don't know how to find the contour with the largest area.
@dervas, try harder !
(you really need minimal coding skills here, and can't always rely on th e kindness of strangers)
@berak thank you for you feedback. I already managed the problem by using a filled contourn, cropping and masks. I know fully well that i need to study more and i'm more than willing to do so! I've only solved the problem by repeating things that i saw other people doing on the internet, but maybe someday i can come with my own resolutions for my problems. Thank you again.
@idervas with your code evreythinkg is wrote in your code : fill largest circle and that's your mask image