I am trying to remove isolated pixels from an image.
I thought of using:
cvErode(img, img, 0, 1);
The problem is that I want a kernel of:
0 0 0
0 1 0
0 0 0
I'm unsure how to do that. Can anyone help?
I am trying to remove isolated pixels from an image.
I thought of using:
cvErode(img, img, 0, 1);
The problem is that I want a kernel of:
0 0 0
0 1 0
0 0 0
I'm unsure how to do that. Can anyone help?
You can filter the image with custom kernels in opencv using filter2D function.
Look at the documentation
Documents are always a good source to start from :)
Now you are setting default kernel by passing NULL as 3rd argument.
You should use http://docs.opencv.org/modules/imgproc/doc/filtering.html?highlight=erode#getstructuringelement and pass output as argument for erode function.
If you can not generate your kernel with this function. Simply create IplConvKernel element by hand.
After re-reading the question's title, now I see what you want. You are after the hit-or-miss morphological operator, the kernel you described is actually a 3x3 square perfectly fine for a function that performs a hit-or-miss. It doesn't seem OpenCV support it, but you can perform the equivalent of you what you want by doing a simple analysis of each point neighborhood: if a point isn't connected to any other point, remove it.
Here is my 5 cent event though i dont know openCV at all.
But you should consider looking for a function called "Opening". This in an erosion followed by a dilation. This will remove small isolated pixel. The size of the removed elements will ofcourse depend on the kernel you use.
Another option is finding a function for doing a lowpass filtering of the image.
nomather what you do, it comes down to two steps. call a function to create a kernel. apply the kernel to the image using another function.
Whatever you do! Dont just use an "erode" function. It will also change the elements on the remaining image. In that case you should definitly use the "opening" function.
If you're using the new OpenCV 2.x API, you can do it like this:
cv::Mat kernel = (cv::Mat_<uchar>(3,3) << 0, 0, 0,
0, 1, 0,
0, 0, 0);
cv::erode(img, img, kernel);