3

openCV で複数の画像を 1 つのウィンドウに合成したいと考えています。ある画像に ROI を作成し、別のカラー画像をこの領域に問題なくコピーできることがわかりました。

ソース画像を何らかの処理を行った画像に切り替えてもうまくいきませんでした。

最終的に、src画像をグレースケールに変換したことがわかり、copyToメソッドを使用しても何もコピーされないことがわかりました。

この質問には、グレースケールからカラーへの対応のみを行う基本的なソリューションで答えました。他の Mat 画像タイプを使用する場合は、追加のテストと変換を実行する必要があります。

4

1 に答える 1

4

私の問題は、グレースケール画像をカラー画像にコピーしようとしていたことに気付きました。そのため、最初に適切なタイプに変換する必要がありました。

drawIntoArea(Mat &src, Mat &dst, int x, int y, int width, int height)
{
    Mat scaledSrc;
    // Destination image for the converted src image.
    Mat convertedSrc(src.rows,src.cols,CV_8UC3, Scalar(0,0,255));

    // Convert the src image into the correct destination image type
    // Could also use MixChannels here.
    // Expand to support range of image source types.
    if (src.type() != dst.type())
    {
        cvtColor(src, convertedSrc, CV_GRAY2RGB);
    }else{
        src.copyTo(convertedSrc);
    }

    // Resize the converted source image to the desired target width.
    resize(convertedSrc, scaledSrc,Size(width,height),1,1,INTER_AREA);

    // create a region of interest in the destination image to copy the newly sized and converted source image into.
    Mat ROI = dst(Rect(x, y, scaledSrc.cols, scaledSrc.rows));
    scaledSrc.copyTo(ROI);
}

画像ソースの種類が異なることに気付くのにしばらく時間がかかりました。他の処理ステップのために画像をグレースケールに変換したことを忘れていました。

于 2012-12-15T15:25:07.263 に答える