Ask Your Question
0

Detect objects even though they touch the border of an image

asked 2020-09-22 09:21:44 -0600

Renos gravatar image

updated 2020-09-24 02:57:56 -0600

Hello to every one,

It is my first post. I am new to image recognition and OpenCV. I am trying to detect pills from an image and I achieved detect most of them. However, some pills touch the border of the image and I have an issue that my algorithm considers the pill with the circle border as one. Please, there is any solution to this problem. I have not found any.

Best regards.

color image

binarized image

edit retag flag offensive close merge delete

Comments

please show, what you have, so far (code/images)

berak gravatar imageberak ( 2020-09-23 01:36:08 -0600 )edit

class ImageDetection:
    def preprocessing(image_path):
        img = cv2.imread(image_path)
        img_blur = cv2.bilateralFilter(img, d = 7, sigmaSpace = 75, sigmaColor = 75)
        gray_img = (cv2.cvtColor(img_blur, cv2.COLOR_BGR2GRAY))
        a = gray_img.max() 
_, thresh = cv2.threshold(gray_img, a/2+60, a, cv2.THRESH_OTSU) (h, w) = thresh.shape[:2] cv2.circle(thresh, ((w//2)+10, (h//2)+1), 500, (0, 1, 255), 59) image, contours, hierarchy = cv2.findContours(image=thresh.copy(), mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE) contours = sorted(contours, key = cv2.contourArea, reverse = True)

Renos gravatar imageRenos ( 2020-09-23 07:35:04 -0600 )edit

Also, I would like to ask. When two objects overlapping. How can I separate them?

Renos gravatar imageRenos ( 2020-09-23 07:39:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-09-24 03:54:25 -0600

updated 2020-09-24 14:05:44 -0600

@Renos this is not an answer. I edited your question and added your code (slightly modified) hoping to be useful...

image description

import cv2
import numpy as np

img = cv2.imread('16009341312672254.jpg')
img_blur = cv2.bilateralFilter(img, d = 7, sigmaSpace = 75, sigmaColor =75)
gray_img = (cv2.cvtColor(img_blur, cv2.COLOR_BGR2GRAY))
a = gray_img.max()  
_, thresh = cv2.threshold(gray_img, a/2+60, a, cv2.THRESH_BINARY)
(h, w) = thresh.shape[:2]
cv2.circle(thresh, ((w//2)+10, (h//2)+1), 500, (255, 255, 255), 100)
kernel = np.ones((15,15),np.uint8)
thresh0 = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
thresh0 = cv2.morphologyEx(thresh0, cv2.MORPH_DILATE, kernel,iterations=3)
cv2.imshow("thresh", thresh)
cv2.imshow("thresh0", thresh0)
contours, hierarchy = cv2.findContours(thresh0, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key = cv2.contourArea, reverse = True)

image_copy = img.copy()
final_image = cv2.drawContours(image_copy, contours, contourIdx=-1, color=(0,255,0), thickness=2)
cv2.imshow("image", image_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

Thank you Suleyman, it is a good example but still when two pills touch each other. The algorithm considers them as one object.

Renos gravatar imageRenos ( 2020-09-24 07:16:46 -0600 )edit

Somehow in some pictures, the algorithm works a little bit. However, the green circle line does not fit exactly around the pill. There is any chance to fit exactly the line?

Renos gravatar imageRenos ( 2020-09-28 07:42:01 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-09-22 09:21:44 -0600

Seen: 1,937 times

Last updated: Sep 24 '20