//形态学约束击中-击不中变换 针对二值和灰度图像 void lhMorpHMTC(const IplImage* src, IplImage* dst, IplConvKernel* sefg, IplConvKernel* sebg =NULL) { assert(src != NULL && dst != NULL && src != dst && sefg!= NULL && sefg!=sebg); if (sebg == NULL) { sebg = lhStructuringElementNot(sefg); } IplImage* temp1 = cvCreateImage(cvGetSize(src), 8, 1); IplImage* temp2 = cvCreateImage(cvGetSize(src), 8, 1); IplImage* temp3 = cvCreateImage(cvGetSize(src), 8, 1); IplImage* temp4 = cvCreateImage(cvGetSize(src), 8, 1); IplImage* mask1 = cvCreateImage(cvGetSize(src), 8, 1); IplImage* mask2 = cvCreateImage(cvGetSize(src), 8, 1); IplImage* mask3 = cvCreateImage(cvGetSize(src), 8, 1); IplImage* mask4 = cvCreateImage(cvGetSize(src), 8, 1); cvZero(mask1); cvZero(mask2); cvZero(mask3); cvZero(mask4); cvZero(dst); //P107 (5.5) cvErode( src, temp1, sebg); cvDilate( src, temp2, sebg); cvErode( src, temp3, sefg); cvDilate( src, temp4, sefg); cvCmp(src, temp3, mask1, CV_CMP_EQ); cvCmp(temp2, src, mask2, CV_CMP_LT); cvAnd(mask1, mask2, mask2); cvCmp(src, temp4, mask3 , CV_CMP_EQ); cvCmp(temp1, src, mask4 , CV_CMP_GT); cvAnd(mask3, mask4, mask4); cvSub(src, temp2, dst, mask2); cvSub(temp1, src, dst, mask4); cvReleaseImage(&mask1); cvReleaseImage(&mask2); cvReleaseImage(&mask3); cvReleaseImage(&mask4); cvReleaseImage(&temp1); cvReleaseImage(&temp2); cvReleaseImage(&temp3); cvReleaseImage(&temp4); cvReleaseStructuringElement(&sebg); }
//形态学非约束击中-击不中变换 针对二值和灰度图像 void lhMorpHMTU(const IplImage* src, IplImage* dst, IplConvKernel* sefg, IplConvKernel* sebg =NULL) { assert(src != NULL && dst != NULL && src != dst && sefg!= NULL && sefg!=sebg); if (sebg == NULL) { sebg = lhStructuringElementNot(sefg); } IplImage* temp = cvCreateImage(cvGetSize(src), 8, 1); IplImage* mask = cvCreateImage(cvGetSize(src), 8, 1); cvZero(mask); //P106 (5.4) cvErode( src, temp, sefg); cvDilate(src, dst, sebg); cvCmp(temp, dst, mask, CV_CMP_GT); cvSub(temp, dst, dst, mask); cvNot(mask, mask); cvSet(dst, cvScalar(0), mask); //cvCopy(dst, mask); //cvSet(dst, cvScalar(255), mask); cvReleaseImage(&mask); cvReleaseImage(&temp); cvReleaseStructuringElement(&sebg); }
int main() { IplImage* img=cvLoadImage("3.7.jpg"); if(!img) { printf("Connot open the image\n"); return -1; } IplImage* rImg=cvCreateImage(cvGetSize(img),img->depth,1); IplImage* gImg=cvCreateImage(cvGetSize(img),img->depth,1); IplImage* bImg=cvCreateImage(cvGetSize(img),img->depth,1); IplImage* clone1=cvCreateImage(cvGetSize(img),img->depth,1); IplImage* clone2=cvCreateImage(cvGetSize(img),img->depth,1); double green_maxPixel=0; double green_minPixel=0; double thresh; cvSplit(img,rImg,gImg,bImg,0); cvNamedWindow("GreenImage0"); cvShowImage("GreenImage0",gImg); cvCopy(gImg,clone1); cvCopy(gImg,clone2); cvMinMaxLoc(gImg,&green_maxPixel,&green_minPixel); thresh=(unsigned char)(green_maxPixel-green_minPixel)/2.0; cvSet(clone1,cvScalar(thresh)); cvZero(clone2); cvCmp(gImg,clone1,clone2,CV_CMP_GE);//掩码标记 cvSubS(gImg,cvScalar(thresh/2),gImg,clone2);//两个矩阵相减 cvNamedWindow("GreenImage1"); cvShowImage("GreenImage1",gImg); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&rImg); cvReleaseImage(&gImg); cvReleaseImage(&bImg); cvDestroyWindow("GreenImage0"); cvDestroyWindow("GreenImage1"); return 0; }
//形态学测地自对偶和自对偶重建运算 void lhMorpRSelfDual(const IplImage* src, const IplImage* msk, IplImage* dst, IplConvKernel* se = NULL, int iterations=-1) { assert(src != NULL && msk != NULL && dst != NULL && src != dst ); //p140(6.7) p142 (6.10) IplImage* temp1 = cvCreateImage(cvGetSize(src), 8, 1); IplImage* temp2 = cvCreateImage(cvGetSize(src), 8, 1); cvZero(temp2); lhMorpRDilate(src, msk, temp1, se, iterations); lhMorpRErode(src, msk, dst, se, iterations); cvCmp(src, msk, temp2, CV_CMP_LE); cvCopy(temp1, dst, temp2); cvReleaseImage(&temp1); cvReleaseImage(&temp2); }