OpenCV去除边缘斑点

OpenCV remove the edge spots?

本文关键字:斑点 边缘 OpenCV      更新时间:2023-10-16

我尝试使用以下代码去除斑点,右上角边缘的斑点,一直无法去除,该怎么办?

CvContourScanner scanner = NULL; scanner = cvStartFindContours(img_src,storage,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_NONE,cvPoint(0,0));

CvRect rect;
while (contour=cvFindNextContour(scanner))
{
    tmparea = fabs(cvContourArea(contour));
    rect = cvBoundingRect(contour,0);   
    if (tmparea < minarea/*||tmparea>4900*/)
    {
        pp=(uchar*)(img_Clone->imageData + img_Clone->widthStep*(rect.y+rect.height/2)+rect.x+rect.width/2);
        if (pp[0]==0)
        {
            for(int y = rect.y;y<rect.y+rect.height;y++)
            {
                for(int x =rect.x;x<rect.x+rect.width;x++)
                {
                    pp=(uchar*)(img_Clone->imageData + img_Clone->widthStep*y+x);
                    if (pp[0]==0)
                    {
                        pp[0]=255;
                    }
                }
            }
        }
    }
}
代码

#include "stdafx.h"
#include <opencv2/opencv.hpp>  
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
# include <stdio.h>
# include <math.h>
#include <cv.h>
void main()
 {
    cv::Mat threshold_output;
    IplImage* img_src;
    CvSeq* contour = NULL;
    double minarea = 100.0;
    double tmparea = 0.0;
    CvMemStorage* storage = cvCreateMemStorage(0);
    IplImage* img_temp= cvLoadImage("C:\temp\original.bmp",CV_LOAD_IMAGE_GRAYSCALE);
    cvErode(img_temp, img_temp, 0, 1);
    threshold(cv::cvarrToMat(img_temp), threshold_output, 200,255, THRESH_BINARY );
    img_src=&IplImage(threshold_output);
    IplImage* img_Clone=cvCloneImage(img_src);
    uchar *pp;  
    IplImage* img_dst = cvCreateImage(cvGetSize(img_src),IPL_DEPTH_8U,1);
    CvScalar color = cvScalar(255,0,0);//CV_RGB(128,0,0);
    CvContourScanner scanner = NULL;
    scanner = cvStartFindContours(img_src,storage,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_NONE,cvPoint(0,0));
    CvRect rect;
    while (contour=cvFindNextContour(scanner))
    {
        tmparea = fabs(cvContourArea(contour));
        rect = cvBoundingRect(contour,0);   
        if (tmparea < minarea/*||tmparea>4900*/)
        {
            pp=(uchar*)(img_Clone->imageData + img_Clone->widthStep*(rect.y+rect.height/2)+rect.x+rect.width/2);
            if (pp[0]==0)
            {
                for(int y = rect.y;y<rect.y+rect.height;y++)
                {
                    for(int x =rect.x;x<rect.x+rect.width;x++)
                    {
                        pp=(uchar*)(img_Clone->imageData + img_Clone->widthStep*y+x);
                        if (pp[0]==0)
                        {
                            pp[0]=255;
                        }
                    }
                }
            }
        }
    }
    cvSaveImage("c://temp//result.bmp",img_Clone);
 }