Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

merge overlapping rectangles

Hello, I am new to OpenCV and I have been trying to detect WBCs. So far, so good but I encountered a problem. There are cells where they are not detected as one and it causes to draw 2 rectangles instead of just one. How can I merge the overlapping rectangles? I found out that OpenCV have a groupRectangles function, can this solve my problem and how do I use it (I can't find an example)? I am using Python 3.7.

Here is the output image of my code: output image of my code

Here is my code:

import cv2
import numpy as np

limit_area = 1000   
x = 0   
y = 0   
w = 0   
h = 0   
nuclei = []   
count = 0   
number_name = 1   

img = cv2.imread('7.bmp')
img = cv2.add(img, 0.70)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)   
mask1 = cv2.inRange(img_hsv, (90,90,0), (255,255,255))  
mask2 = cv2.inRange(img_hsv, (70,100,0), (255,255,255)) 
mask = mask1 + mask2  
kernel = np.ones((1,3),np.uint8)  
mask = cv2.dilate(mask,kernel,iterations = 2)   
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)  
mask_blur = cv2.medianBlur(mask,5)  
canny = cv2.Canny(mask_blur, 100,300,3) 
im2, contours, hierarchy = cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)  

for cnt in contours:   
    if cv2.contourArea(cnt) >= limit_area:   
        nuclei.append(cnt)
        print(cv2.contourArea(cnt))
        x, y, w, h = cv2.boundingRect(cnt)
        cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,0), 7)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
click to hide/show revision 2
retagged

updated 2018-12-02 02:25:10 -0600

berak gravatar image

merge overlapping rectangles

Hello, I am new to OpenCV and I have been trying to detect WBCs. So far, so good but I encountered a problem. There are cells where they are not detected as one and it causes to draw 2 rectangles instead of just one. How can I merge the overlapping rectangles? I found out that OpenCV have a groupRectangles function, can this solve my problem and how do I use it (I can't find an example)? I am using Python 3.7.

Here is the output image of my code: output image of my code

Here is my code:

import cv2
import numpy as np

limit_area = 1000   
x = 0   
y = 0   
w = 0   
h = 0   
nuclei = []   
count = 0   
number_name = 1   

img = cv2.imread('7.bmp')
img = cv2.add(img, 0.70)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)   
mask1 = cv2.inRange(img_hsv, (90,90,0), (255,255,255))  
mask2 = cv2.inRange(img_hsv, (70,100,0), (255,255,255)) 
mask = mask1 + mask2  
kernel = np.ones((1,3),np.uint8)  
mask = cv2.dilate(mask,kernel,iterations = 2)   
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)  
mask_blur = cv2.medianBlur(mask,5)  
canny = cv2.Canny(mask_blur, 100,300,3) 
im2, contours, hierarchy = cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)  

for cnt in contours:   
    if cv2.contourArea(cnt) >= limit_area:   
        nuclei.append(cnt)
        print(cv2.contourArea(cnt))
        x, y, w, h = cv2.boundingRect(cnt)
        cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,0), 7)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

merge overlapping rectangles

Hello, I am new to OpenCV and I have been trying to detect WBCs. So far, so good but I encountered a problem. There are cells where they are not detected as one and it causes to draw 2 rectangles instead of just one. How can I merge the overlapping rectangles? I found out that OpenCV have a groupRectangles function, can this solve my problem and how do I use it (I can't find an example)? I am using Python 3.7.

Here is the output image of my code: output image of my code

Here is my code:

import cv2
import numpy as np

limit_area = 1000   
x = 0   
y = 0   
w = 0   
h = 0   
nuclei = []   
count = 0   
number_name = 1   

img = cv2.imread('7.bmp')
img = cv2.add(img, 0.70)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)   
mask1 = cv2.inRange(img_hsv, (90,90,0), (255,255,255))  
mask2 = cv2.inRange(img_hsv, (70,100,0), (255,255,255)) 
mask = mask1 + mask2  
kernel = np.ones((1,3),np.uint8)  
mask = cv2.dilate(mask,kernel,iterations = 2)   
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)  
mask_blur = cv2.medianBlur(mask,5)  
canny = cv2.Canny(mask_blur, 100,300,3) 
im2, contours, hierarchy = cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)  

for cnt in contours:   
    if cv2.contourArea(cnt) >= limit_area:   
        nuclei.append(cnt)
        print(cv2.contourArea(cnt))
        x, y, w, h = cv2.boundingRect(cnt)
        cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,0), 7)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()