opencv ---冈萨雷斯《图像处理》课后作业
显微应用中一个预处理步骤是从两组或更多组重叠的类似颗粒中分离出单个独立的一种圆颗粒。假设所有颗粒的大小都相同,提出一种产生3幅图像的形态学算法,这3幅图像分别仅由如下物体组成:
(a)仅与图像边界融合一块的颗粒
(b)仅彼此重叠的颗粒
(c)没有重叠的颗粒
形态学算法想不出解决办法,只好用contours来解决。
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat srcImage = imread("D:\\picture\\picture9\\1.tif", 0);
imshow("原图", srcImage);
Mat dstImage1, dstImage2,midImage;
Mat dstImage3 = srcImage.clone();
dstImage3 = Scalar::all(0);
dstImage1 = dstImage3.clone();
dstImage3.copyTo(dstImage2);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(srcImage, midImage, MORPH_ERODE,element);
Canny(midImage, srcImage, 50, 150, 3);
imshow("dafas", srcImage);
findContours(srcImage, contours,hierarchy, RETR_CCOMP, CHAIN_APPROX_NONE);
for (int i = 0; i < contours.size(); ++i)
{
double Area = contourArea(contours[i]);
if (Area >350 )
{
drawContours(dstImage1, contours, i, 255, CV_FILLED, 8, hierarchy);
}
else if (Area <350 && Area >150)
{
drawContours(dstImage2, contours, i, 255, CV_FILLED, 8, hierarchy);
}
}
absdiff(midImage, dstImage1, dstImage3);
absdiff(dstImage3, dstImage2, dstImage3);
morphologyEx(dstImage1, dstImage1, MORPH_DILATE, element);
morphologyEx(dstImage2, dstImage2, MORPH_DILATE, element);
morphologyEx(dstImage3, dstImage3, MORPH_OPEN, element,Point(1,1),2);
imshow("粘连的点", dstImage1);
imshow("单个点", dstImage2);
imshow("边界的点", dstImage3);
waitKey(0);
}