0

私はopencvが初めてです。私の質問は次のとおりです。私は大きなイメージを持っています。各コーナーから 5X5 ピクセルの領域を取得するように、4 つのコーナーを取得したいと思います。それでは、四隅を元に一つの大きな画像を作りたいと思います。

新しい画像は 10X10 のサイズになります。

これを回避する最善の方法は何でしょうか?

ありがとう

4

2 に答える 2

4

C++ インターフェイスを使用すると、次のように実行できます。

#include <opencv2/opencv.hpp>
cv::Mat CornersOnly(const cv::Mat& src, int cw, int ch)
{
    using namespace cv;
    int w(src.cols);
    int h(src.rows);
    CV_Assert(w >= cw);
    CV_Assert(h >= ch);
    Mat dst(2*ch, 2*cw, src.type());
    Mat(src, Rect(0,    0,    cw, ch)).copyTo(Mat(dst, Rect( 0,  0, cw, ch)));
    Mat(src, Rect(w-cw, 0,    cw, ch)).copyTo(Mat(dst, Rect(cw,  0, cw, ch)));
    Mat(src, Rect(0,    h-ch, cw, ch)).copyTo(Mat(dst, Rect( 0, ch, cw, ch)));
    Mat(src, Rect(w-cw, h-ch, cw, ch)).copyTo(Mat(dst, Rect(cw, ch, cw, ch)));
    return dst;
}
int main()
{
    cv::imwrite("Lenna_Corners.png", CornersOnly(cv::imread("Lenna.png"), 100, 100));
}

入力:

ここに画像の説明を入力

出力:

ここに画像の説明を入力

見やすくするために、この例では 5 ピクセルではなく 100 ピクセルを使用しました。

于 2013-01-12T16:31:43.293 に答える
1

OpenCV C インターフェイスで画像の 4 つのコーナーを組み合わせて画像を作成する簡単な関数を次に示します。それが最善の方法かどうかはわかりませんが、うまくいきます:

IplImage* getCorners(IplImage* img, int regionWidth, int regionHeight)
{
    IplImage* result = cvCreateImage(cvSize(regionWidth * 2,regionHeight * 2),img->depth,img->nChannels);

    //Copy Top Left Region
    cvSetImageROI(img,cvRect(0,0,regionWidth,regionHeight));
    cvSetImageROI(result,cvRect(0,0,regionWidth,regionHeight));
    cvCopy(img,result);

    //Copy Top Right Region
    cvSetImageROI(img,cvRect(img->width - regionWidth - 1,0,regionWidth,regionHeight));
    cvSetImageROI(result,cvRect(regionWidth,0,regionWidth,regionHeight));
    cvCopy(img,result);

    //Copy Bottom Left Region
    cvSetImageROI(img,cvRect(0,img->height - regionHeight - 1,regionWidth,regionHeight));
    cvSetImageROI(result,cvRect(0,regionHeight,regionWidth,regionHeight));
    cvCopy(img,result);

    //Copy Bottom Right Region
    cvSetImageROI(img,cvRect(img->width - regionWidth - 1,img->height - regionHeight - 1,regionWidth,regionHeight));
    cvSetImageROI(result,cvRect(regionWidth,regionHeight,regionWidth,regionHeight));
    cvCopy(img,result);

    //Reset Image Region Of Interest
    cvResetImageROI(img);
    cvResetImageROI(result);

    return result;
}

お役に立てば幸いです。:)

于 2013-01-12T15:22:10.077 に答える