opencv截取图像,并且比较是否相同

opencv 3和opencv 4不一样,用法头文件等会有差异,需要自行调整
本案例所使用的是4

初级理论:图像文件比较

#include 
#include 

using namespace cv;
using namespace std;

int main(int argc, const char* argv[])
{
	//创建第1张矩形图
	Mat src1 = imread("D:\\Desktop\\Handwritten-Numeral-Recognition-master\\他.png");


	//创建第2张矩形图
	Mat src2 = imread("D:\\Desktop\\Handwritten-Numeral-Recognition-master\\他2.png");

	//两张图像素逻辑异或操作
	Mat dst2;
	bitwise_xor(src1, src2, dst2);

	imshow("异或后的图像", dst2);
	waitKey(0);

	//遍历, 遍历dst2的每个点,判断是否为黑色(0,0,0);
	ushort d;
	for (int i = 0; i < dst2.rows - 1; i++)
	{
		for (int j = 0; j < dst2.cols-1; j++)
		{
			d= dst2.ptr<ushort>(i)[j];
			if (d != 0)
			{
				cout << "出现了不一致,两张图片不一样" << endl;
				return 0;
			}
		}
	}
	cout << "俩图片完全一致" << endl;
	return 0;
}

初级理论:屏幕截取(这部分是看大佬写的)

Screenshot.c

#include "Screenshot.h"
using cv::Mat;

Screenshot::Screenshot()
{
    double zoom = getZoom();
    m_width = GetSystemMetrics(SM_CXSCREEN) * zoom;
    m_height = GetSystemMetrics(SM_CYSCREEN) * zoom;
    m_screenshotData = new char[m_width * m_height * 4];
    memset(m_screenshotData, 0, m_width);

    // 获取屏幕 DC
    m_screenDC = GetDC(NULL);
    m_compatibleDC = CreateCompatibleDC(m_screenDC);

    // 创建位图
    m_hBitmap = CreateCompatibleBitmap(m_screenDC, m_width, m_height);
    SelectObject(m_compatibleDC, m_hBitmap);
}

/* 获取整个屏幕的截图 */
Mat Screenshot::getScreenshot()
{
    // 得到位图的数据
    BitBlt(m_compatibleDC, 0, 0, m_width, m_height, m_screenDC, 0, 0, SRCCOPY);
    GetBitmapBits(m_hBitmap, m_width * m_height * 4, m_screenshotData);

    // 创建图像
    Mat screenshot(m_height, m_width, CV_8UC4, m_screenshotData);

    return screenshot;
}

/** @brief 获取指定范围的屏幕截图
 * @param x 图像左上角的 X 坐标
 * @param y 图像左上角的 Y 坐标
 * @param width 图像宽度
 * @param height 图像高度
 */
Mat Screenshot::getScreenshot(int x, int y, int width, int height)
{
    Mat screenshot = getScreenshot();
    return screenshot(cv::Rect(x, y, width, height));
}

/* 获取屏幕缩放值 */
double Screenshot::getZoom()
{
    // 获取窗口当前显示的监视器
    HWND hWnd = GetDesktopWindow();
    HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);

    // 获取监视器逻辑宽度
    MONITORINFOEX monitorInfo;
    monitorInfo.cbSize = sizeof(monitorInfo);
    GetMonitorInfo(hMonitor, &monitorInfo);
    int cxLogical = (monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left);

    // 获取监视器物理宽度
    DEVMODE dm;
    dm.dmSize = sizeof(dm);
    dm.dmDriverExtra = 0;
    EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &dm);
    int cxPhysical = dm.dmPelsWidth;

    return cxPhysical * 1.0 / cxLogical;
}

Screenshot.h

#pragma once
#include 
#include 

class Screenshot
{
public:
    Screenshot();
    double static getZoom();
    cv::Mat getScreenshot();
    cv::Mat getScreenshot(int x, int y, int width, int height);

private:
    int m_width;
    int m_height;
    HDC m_screenDC;
    HDC m_compatibleDC;
    HBITMAP m_hBitmap;
    LPVOID m_screenshotData = nullptr;
};


main.c

#include 
#include 
#include "Screenshot.h"
int main()
{
	Screenshot screenshot;
	// 截取范围
	Mat mat = screenshot.getScreenshot(922, 479, 30, 30);
	return 0;
}

完整代码实现

#include 
#include 
#include "Screenshot.h"
#include 

using namespace cv;
using namespace std;

int main(int argc, const char* argv[])
{
	Screenshot screenshot;
	Mat src2 = screenshot.getScreenshot(922, 479, 30, 30);
	while (true)
	{
		//截取图像
		Sleep(5000);
		Mat src1 = screenshot.getScreenshot(922, 479, 30, 30);
		

		//两张图像素逻辑异或操作
		Mat dst2;
		bitwise_xor(src1, src2, dst2);

		/*imshow("异或后的图像", dst2);
		waitKey(0);*/

		//遍历, 遍历dst2的每个点,判断是否为黑色(0,0,0);
		ushort d;
		for (int i = 0; i < dst2.rows - 1; i++)
		{
			for (int j = 0; j < dst2.cols - 1; j++)
			{
				d = dst2.ptr<ushort>(i)[j];
				if (d != 0)
				{
					cout << "出现了不一致,两张图片不一样" << endl;
					
					return 0;
				}
			}
		}
		cout << "俩图片完全一致" << endl;

		// 将新图像赋给src2,等下一轮截取,再次比较
		src2 = src1;
	}
	return 0;
}```

你可能感兴趣的:(有趣的cpp,opencv,计算机视觉,人工智能)