So, I'm working on finding the small components on an electric chip that can be seen in the image. Till now, I have been working on finding the contours and then applying the Morphology operation and drawing the rectangles. I am attaching the original, required, and achieved image along with code so that it can be easy for the community to understand the problem.
import cv2
import os
# Load iamge, grayscale, adaptive threshold
image = cv2.imread('4.jpg')
result = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV,51,9)
# Fill rectangular contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(thresh, [c], -1, (255,255,255), -1)
# Morph opend
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=4)
# Draw rectangles
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x w, y h), (36,255,12), 3)
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('image', image)
cv2.waitKey()
This is the result that I am trying to achieve
Any help will be appreciated. Thanks
CodePudding user response:
Template matching could be already enough. Just give it a try with the ROI as the template image. You may vary the score to get the tilted elements.
Have a look here: https://docs.opencv.org/master/d4/dc6/tutorial_py_template_matching.html
CodePudding user response:
i think you can use matchTemplate, but while you use the function, you will get more then one similar point,
for example, your image have three object, the first object while have multi similarity points, it depends on your threshold, and you need to eliminate that.
in my experience, i use half size of the target image to be the bound distance, sorting the result point, then suppose the first point is the best point, then eliminate the similarity point inside bound distance near the best point. the next point will be your second object's point.