OpenCV: assertation failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.t
OpenCV: assertation failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type())
我从上面收到此错误,但不知道如何避免它。我的目标是获取屏幕截图,然后对其执行模板匹配,以查看此时屏幕上是否显示图标。到目前为止,它只是图标的位置。我的代码:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
using namespace cv;
Mat hwnd2mat();
/// Global Variables
Mat img; Mat templ; Mat result;
int main()
{
/// Load image and template
templ = imread( "Template.bmp",1);
templ.convertTo(templ, CV_8U);
//img = imread( "Image.jpg", 1 );
img = hwnd2mat();
/// Create the result matrix
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
result.create( result_cols, result_rows, CV_8U);
/// Do the Matching and Normalize
matchTemplate( img, templ, result, CV_TM_SQDIFF );
normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
/// Localizing the best match with minMaxLoc
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_8U);
// 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;
// use the previously created device context with the bitmap
SelectObject(hwindowCompatibleDC, hbwindow);
// copy from the window device context to the bitmap device context
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;
}
带有屏幕截图的功能不是我自己的作品,我从这里得到的
有什么想法怎么办?
感谢您的帮助,最好的问候!
问题是函数
hwnd2mat
返回的是 CV_8UC1
类型的灰度图像,而templ
是 CV_8UC3
类型的彩色图像。因此,由于失败条件img.type() == templ.type()
,断言在函数matchTemplate
上失败。您可以将图像加载为灰度以避免错误。
templ = imread( "Template.bmp",CV_LOAD_IMAGE_GRAYSCALE);
更新:
值得注意的是,函数hwnd2mat
以其当前形式不起作用,并且返回无效图像。原始代码创建类型 CV_8UC4
的输出图像,这是正确的方法。
src.create(height,width,CV_8UC4);
您可以在从hwnd2mat
返回之前将src
转换为灰度,也可以将templ
转换为 4 通道图像。无论如何,关键是两个图像必须具有相同的类型才能matchTemplate
工作。
相关文章:
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 概念中的cv限定符需要表达式参数列表
- 将CHW格式的浮点向量转换为cv::Mat
- 错误的cv::face FacemarkLBF实例化
- 如何检查给定的参数是否为 cv::noArray()?
- 开放 CV 中的动态内存分配,用于视频处理
- 如何在 opencv 中使用 cv::VideoCapture::waitAny()
- 错误:未定义对cv::cudacodec::createVideoReader的引用
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- C++:从GPU内存(cudaMemcpy2D)获取BGR图像(cv::Mat)
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 将 cv::mat 转换为 QImage
- Inference pytorch C++ with alexnet and cv::imread image
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用
- cv::Normalise() 中的 L2_NORM 和 NORM_MINMAX 实现有什么区别?
- 将 cv::Mat 转换为 std::vector 的通用函数
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- 如何将 cv::Mat & Img 复制到未签名的 char* img?opencv
- 如何在C++中声明一个定义为func(cv::Mat &img)的函数
- 使用 Img.at<cv::vec3 获取无符号短值???>