c++ 實現 膨脹(dilate)腐蝕(erode)
阿新 • • 發佈:2019-01-07
int main() { Mat img=imread("lena.jpg"); cv::cvtColor(img,img,CV_BGR2GRAY); int imageWidth = img.cols; int imageHeight = img.rows; showImage("src",img); uchar * imageBuffer = (uchar *) malloc(imageHeight * imageWidth); memset(imageBuffer,0,imageHeight * imageWidth); if (img.depth() == 3) { } else { for (int i = 0; i< imageHeight; i++) { for (int j = 0; j < imageWidth;j++) { imageBuffer[i*imageWidth + j] = img.at(i,j); } } dilateTest(imageBuffer,imageBuffer,imageWidth,imageHeight); for (int i = 0; i< imageHeight; i++) { for (int j = 0; j < imageWidth;j++) { img.at(i,j) = imageBuffer[i*imageWidth + j]; } } } Mat copyImage;// = img.clone(); //與opencv dilate 比較 //int dilation_type =0; //int //if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; } //else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; } //else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; } //Mat element = getStructuringElement( 0, // Size( 2*0 + 1, 2*0+1 ), // Point( 0, 0 ) ); /////膨脹操作 //dilate( img, copyImage, element ); //showImage("copyImage",copyImage); showImage("testcv",img); waitKey(100000); } void dilateTest(uchar *imageBuffer,uchar *outBuffer,int imageWidth,int imageHeight) { uchar *dilateBuffer = (uchar *)malloc((imageWidth+1)*(1+imageHeight)); memset(dilateBuffer,0,(imageHeight+1)*(imageWidth+1)); for (int i = 0;i < imageHeight;i++) { for (int j = 0 ; j < imageWidth; j++) { dilateBuffer[i*(imageWidth+1)+j+1] = imageBuffer[i*imageWidth + j]; } } uchar *srcImage = dilateBuffer; for (int i = 0;i < imageWidth; i++) { for (int j = 0;j < imageHeight;j++) { uchar tempNum = 0; srcImage = (dilateBuffer + (i*(imageWidth +1)+j)); for (int m = 0;m<3;m++) { for (int n = 0; n < 3;n++) { if (tempNum < srcImage[n]) { tempNum = srcImage[n]; } } srcImage = (srcImage + m*(imageWidth +1)); } outBuffer[i*imageWidth +j] = tempNum; } } }