1

私はopenCVの初心者です。すでに紙の端を検出していますが、端に線を引いた後、結果の画像がぼやけています。

私が欠けているもの..

私のコードは以下です。

どうもありがとう。

ここに画像の説明を入力

-(void)forOpenCV
{
   if( imageView.image != nil )
   {

      cv::Mat greyMat=[self cvMatFromUIImage:imageView.image];
      vector<vector<cv::Point> > squares;

      cv::Mat img= [self debugSquares: squares: greyMat ];


      imageView.image =[self UIImageFromCVMat: img];

   }

}


- (cv::Mat) debugSquares: (std::vector<std::vector<cv::Point> >) squares : (cv::Mat &)image
{
NSLog(@"%lu",squares.size());

// blur will enhance edge detection

Mat blurred(image);
medianBlur(image, blurred, 9);

Mat gray0(image.size(), CV_8U), gray;
vector<vector<cv::Point> > contours;

// find squares in every color plane of the image
for (int c = 0; c < 3; c++)
{
    int ch[] = {c, 0};
    mixChannels(&image, 1, &gray0, 1, ch, 1);

    // try several threshold levels
    const int threshold_level = 2;
    for (int l = 0; l < threshold_level; l++)
    {
        // Use Canny instead of zero threshold level!
        // Canny helps to catch squares with gradient shading
        if (l == 0)
        {
            Canny(gray0, gray, 10, 20, 3); //

            // Dilate helps to remove potential holes between edge segments
            dilate(gray, gray, Mat(), cv::Point(-1,-1));
        }
        else
        {
            gray = gray0 >= (l+1) * 255 / threshold_level;
        }

        // Find contours and store them in a list
        findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        // Test contours
        vector<cv::Point> approx;
        for (size_t i = 0; i < contours.size(); i++)
        {
            // approximate contour with accuracy proportional
            // to the contour perimeter
            approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

            // Note: absolute value of an area is used because
            // area may be positive or negative - in accordance with the
            // contour orientation
            if (approx.size() == 4 &&
                fabs(contourArea(Mat(approx))) > 1000 &&
                isContourConvex(Mat(approx)))
            {
                double maxCosine = 0;

                for (int j = 2; j < 5; j++)
                {
                    double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                    maxCosine = MAX(maxCosine, cosine);
                }

                if (maxCosine < 0.3)
                    squares.push_back(approx);
            }
        }
    }
}

NSLog(@"%lu",squares.size());


for( size_t i = 0; i < squares.size(); i++ )
{


    cv:: Rect rectangle = boundingRect(Mat(squares[i]));
    if(i==squares.size()-1)////Detecting Rectangle here
    {
        const cv::Point* p = &squares[i][0];


        int n = (int)squares[i].size();

         NSLog(@"%d",n);



        line(image, cv::Point(507,418), cv::Point(507+1776,418+1372), Scalar(255,0,0),2,8);

        polylines(image, &p, &n, 1, true, Scalar(255,255,0), 5, CV_AA);



        fx1=rectangle.x;
        fy1=rectangle.y;
        fx2=rectangle.x+rectangle.width;
        fy2=rectangle.y+rectangle.height;


        line(image, cv::Point(fx1,fy1), cv::Point(fx2,fy2), Scalar(0,0,255),2,8);


    }



}


return image;
}
4

2 に答える 2

1

それ以外の

Mat blurred(image);

あなたがする必要があります

Mat blurred = image.clone();

最初の行は画像をコピーするのではなく、同じデータへの 2 番目のポインターを作成するだけだからです。画像をぼかすと、元の画像も変更されます。代わりに、実際のデータの実際のコピーを作成し、このコピーを操作する必要があります。

OpenCV リファレンスには次のように記載されています。

コピー コンストラクターまたは代入演算子を使用して、右側に行列または式を指定できます。以下を参照してください。繰り返しますが、冒頭で述べたように、行列の割り当ては O(1) 操作です。これは、ヘッダーをコピーして参照カウンターを増やすだけだからです。

Mat::clone() メソッドを使用して、必要なときに行列の完全な (別名、深い) コピーを取得できます。

于 2012-11-21T13:07:52.233 に答える
1

最初の問題は、元の画像のコピーに対してすべての処理を行うことで簡単に解決できます。そうすれば、正方形のすべての点を取得した後、元の画像に線を引くことができ、ぼやけません。

クロッピングである 2 番目の問題は、元の画像で ROI (関心領域) を定義し、それを新しい Mat にコピーすることで解決できます。私はこの回答でそれを実証しました:

// Setup a Region Of Interest
cv::Rect roi;
roi.x = 50
roi.y = 10
roi.width = 400;
roi.height = 450;

// Crop the original image to the area defined by ROI
cv::Mat crop = original_image(roi);

cv::imwrite("cropped.png", crop);
于 2012-11-23T17:08:03.070 に答える