OpenCV中文网站

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 安装 配置
查看: 177|回复: 3

matchTemplate 对比问题

[复制链接]
发表于 2017-8-31 16:00:00 | 显示全部楼层 |阅读模式
  1. <div>int main()</div><div>{</div><div><span style="white-space:pre">        </span>/// Load image and template</div><div><span style="white-space:pre">        </span>templ = imread("E:/pic/1.bmp", 0);</div><div><span style="white-space:pre">        </span>Mat temp2;</div><div><span style="white-space:pre">        </span></div><div><span style="white-space:pre">        </span>templ.convertTo(templ, CV_8UC4);</div><div>
  2. </div><div><span style="white-space:pre">        </span>img = hwnd2mat();</div><div><span style="white-space:pre">        </span>imshow("", img);</div><div>
  3. </div><div><span style="white-space:pre">        </span>cout << "templ depth : "<< templ.depth() << "--" << "templ type:" << templ.type() << endl;</div><div>
  4. </div><div><span style="white-space:pre">        </span>/// Create the result matrix</div><div><span style="white-space:pre">        </span>Mat result;</div><div>
  5. </div><div><span style="white-space:pre">        </span>matchTemplate(img, templ, result, CV_TM_SQDIFF);</div><div>
  6. </div><div><span style="white-space:pre">        </span>double minVal; double maxVal; Point minLoc; Point maxLoc;</div><div><span style="white-space:pre">        </span>Point matchLoc;</div><div>
  7. </div><div><span style="white-space:pre">        </span>minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());</div><div>
  8. </div><div><span style="white-space:pre">        </span>/// show best position</div><div><span style="white-space:pre">        </span>matchLoc = minLoc;</div><div><span style="white-space:pre">        </span>cout << matchLoc << " is best position" << endl;</div><div>
  9. </div><div><span style="white-space:pre">        </span>waitKey(0);</div><div><span style="white-space:pre">        </span>return 0;</div><div>}</div><div>
  10. </div><div>//截取屏幕,返回mat对象</div><div>Mat hwnd2mat() {</div><div>
  11. </div><div><span style="white-space:pre">        </span>HWND hwnd = GetDesktopWindow();</div><div><span style="white-space:pre">        </span>HDC hwindowDC, hwindowCompatibleDC;</div><div>
  12. </div><div><span style="white-space:pre">        </span>int height, width, srcheight, srcwidth;</div><div><span style="white-space:pre">        </span>HBITMAP hbwindow;</div><div><span style="white-space:pre">        </span>Mat src;</div><div><span style="white-space:pre">        </span>BITMAPINFOHEADER  bi;</div><div>
  13. </div><div><span style="white-space:pre">        </span>hwindowDC = GetDC(hwnd);</div><div><span style="white-space:pre">        </span>hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);</div><div><span style="white-space:pre">        </span>SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);</div><div>
  14. </div><div><span style="white-space:pre">        </span>RECT windowsize;    // get the height and width of the screen</div><div><span style="white-space:pre">        </span>GetClientRect(hwnd, &windowsize);</div><div>
  15. </div><div><span style="white-space:pre">        </span>srcheight = windowsize.bottom;</div><div><span style="white-space:pre">        </span>srcwidth = windowsize.right;</div><div><span style="white-space:pre">        </span>height = windowsize.bottom / 1;  //change this to whatever size you want to resize to</div><div><span style="white-space:pre">        </span>width = windowsize.right / 1;</div><div>
  16. </div><div><span style="white-space:pre">        </span>src.create(height, width, CV_8UC4);</div><div><span style="white-space:pre">        </span>cout << "src depth : " << src.depth() << "--" << "src type:" << src.type() << endl;</div><div><span style="white-space:pre">        </span>// create a bitmap</div><div><span style="white-space:pre">        </span>hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);</div><div><span style="white-space:pre">        </span>bi.biSize = sizeof(BITMAPINFOHEADER);</div><div><span style="white-space:pre">        </span>bi.biWidth = width;</div><div><span style="white-space:pre">        </span>bi.biHeight = -height;  //this is the line that makes it draw upside down or not</div><div><span style="white-space:pre">        </span>bi.biPlanes = 1;</div><div><span style="white-space:pre">        </span>bi.biBitCount = 32;</div><div><span style="white-space:pre">        </span>bi.biCompression = BI_RGB;</div><div><span style="white-space:pre">        </span>bi.biSizeImage = 0;</div><div><span style="white-space:pre">        </span>bi.biXPelsPerMeter = 0;</div><div><span style="white-space:pre">        </span>bi.biYPelsPerMeter = 0;</div><div><span style="white-space:pre">        </span>bi.biClrUsed = 0;</div><div><span style="white-space:pre">        </span>bi.biClrImportant = 0;</div><div>
  17. </div><div><span style="white-space:pre">        </span>SelectObject(hwindowCompatibleDC, hbwindow);</div><div><span style="white-space:pre">        </span>StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !</div><div><span style="white-space:pre">        </span>GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS);  //copy from hwindowCompatibleDC to hbwindow</div><div>
  18. </div><div><span style="white-space:pre">        </span>DeleteObject(hbwindow);</div><div><span style="white-space:pre">        </span>DeleteDC(hwindowCompatibleDC);</div><div><span style="white-space:pre">        </span>ReleaseDC(hwnd, hwindowDC);</div><div>
  19. </div><div><span style="white-space:pre">        </span>return src;</div><div>}</div>
复制代码

截取屏幕是 从stackoverflow上拿到的代码,他能截取当前屏幕保存为mat格式,但是调用matchtemplate方法会出错,错误如下:
  1. <div>OpenCV Error: Assertion failed ((depth == CV_8U || depth == CV_32F) && type == _</div><div>templ.type() && _img.dims() <= 2) in cv::matchTemplate, file C:\build\master_win</div><div>pack-build-win64-vc14\opencv\modules\imgproc\src\templmatch.cpp, line 1102</div>
复制代码

后来通过查资料知道了想要matchtemplate 必须保证对比的两张图的type一样,于是乎找到convertto函数,但是总得不到结果,求前辈指点,今天刚接触这个框架,暂时还没有细致学习。
回复

使用道具 举报

 楼主| 发表于 2017-8-31 16:01:51 | 显示全部楼层
测试代码如下:

int main()
{
        /// Load image and template
        templ = imread("E:/pic/1.bmp", 0);
        Mat temp2;
       
        templ.convertTo(templ, CV_8UC4);

        img = hwnd2mat();
        imshow("", img);

        cout << "templ depth : "<< templ.depth() << "--" << "templ type:" << templ.type() << endl;

        /// Create the result matrix
        Mat result;

        matchTemplate(img, templ, result, CV_TM_SQDIFF);

        double minVal; double maxVal; Point minLoc; Point maxLoc;
        Point matchLoc;

        minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());

        /// show best position
        matchLoc = minLoc;
        cout << matchLoc << " is best position" << endl;

        waitKey(0);
        return 0;
}

Mat hwnd2mat() {

        HWND hwnd = GetDesktopWindow();
        HDC hwindowDC, hwindowCompatibleDC;

        int height, width, srcheight, srcwidth;
        HBITMAP hbwindow;
        Mat src;
        BITMAPINFOHEADER  bi;

        hwindowDC = GetDC(hwnd);
        hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
        SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);

        RECT windowsize;    // get the height and width of the screen
        GetClientRect(hwnd, &windowsize);

        srcheight = windowsize.bottom;
        srcwidth = windowsize.right;
        height = windowsize.bottom / 1;  //change this to whatever size you want to resize to
        width = windowsize.right / 1;

        src.create(height, width, CV_8UC4);
        cout << "src depth : " << src.depth() << "--" << "src type:" << src.type() << endl;
        // create a bitmap
        hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
        bi.biSize = sizeof(BITMAPINFOHEADER);
        bi.biWidth = width;
        bi.biHeight = -height;  //this is the line that makes it draw upside down or not
        bi.biPlanes = 1;
        bi.biBitCount = 32;
        bi.biCompression = BI_RGB;
        bi.biSizeImage = 0;
        bi.biXPelsPerMeter = 0;
        bi.biYPelsPerMeter = 0;
        bi.biClrUsed = 0;
        bi.biClrImportant = 0;

        SelectObject(hwindowCompatibleDC, hbwindow);
        StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !
        GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS);  //copy from hwindowCompatibleDC to hbwindow

        DeleteObject(hbwindow);
        DeleteDC(hwindowCompatibleDC);
        ReleaseDC(hwnd, hwindowDC);

        return src;
}

错误信息如下:
OpenCV Error: Assertion failed ((depth == CV_8U || depth == CV_32F) && type == _
templ.type() && _img.dims() <= 2) in cv::matchTemplate, file C:\build\master_win
pack-build-win64-vc14\opencv\modules\imgproc\src\templmatch.cpp, line 1102

第一次发帖 格式飞了 抱歉。

回复 支持 反对

使用道具 举报

发表于 2017-8-31 23:34:22 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2017-9-8 20:12:00 | 显示全部楼层
主贴的代码怎么乱七八糟的,请修正。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|感谢博安盈科技资助本网站|OpenCV中文网站 ( 粤ICP备13079875号-2   

GMT+8, 2017-9-22 10:35 , Processed in 0.104432 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表