问题
I have a picure from a microscope. I want to quantify the number of cellular nucli (blue ovals - see picture original image)
For that I am using Python 2.7 with OpenCV3.
First, I load the image in BGR, and extract the blue layer
import cv2
import numpy as np
img = cv2.imread('image.jpg',1)
img_gray = img[:,:,0]
Next, I produce the BINARY image and then I applied morphological opening, filling and measured contours (see picture: GRAY-after thresholding, OP-n:after opening n times, 'filling holes' and measuring contours.
_, img_thres = cv2.threshold(img_gray, 40, 255, cv2.THRESH_BINARY)
For iterations=2 see upper panels (OP-2)
kernel = np.ones((2,2),np.uint8)
img_op = cv2.morphologyEx(img_thres, cv2.MORPH_OPEN, kernel, iterations =2)
or
For iterations=20 see bottom panels (OP-20)
img_op = cv2.morphologyEx(img_thres, cv2.MORPH_OPEN, kernel, iterations =20)
For simplicity I am skiping the filling and contours code.
You can see how the contours are shifted, they shift to the right proportionality to the number of iterations during the opening transformation.
Opening should remove background pixels, as it is doing, but not shift the pixels. What is going on?
****EDIT****
didnt verify it, but maybe it's because your kernel is of even size? try an odd kernel size please like 3 or 5. – Micka
You are right! See pictures (both opening iterations=20):
来源:https://stackoverflow.com/questions/43285609/why-morphological-opening-displace-my-image