因此,Internet上有一个库来处理位图文件:"bitmap_image.hpp"。它似乎很好,它适用于我的项目,直到我尝试在我的项目中包含“Windows.h”。编译器无法识别分别来自“bitmap_image.hpp”和“Windows.h”的“bitmap_image”类和“POINT”结构。
我尝试了所有我知道但问题仍未解决的问题。此外,当尝试编译另一个包含 cstdio,bitmap_image.hpp 和 Windows.h 的项目时,它可以工作。
我使用Qt,这里是所有代码:
的main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtDebug>
#include "bitmap_image.hpp"
#include <Windows.h>
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
double compare_bitmap(bitmap_image& large, bitmap_image& small, unsigned int top_left_x, unsigned int top_left_y)
{
/*
Purpose:
compare two bitmaps and return the percentage of matched pixels
Description:
this function concerns the pixels in "large" only if it falls in this square--
top-left: (top_left_x, top_left_y)
bottom-right: (top_left_x+small.width()-1, top_left_y+small.height()-1)
while all pixels in "small" will be concerned
Rules:
"large" should not be smaller than "small" in either dimensions
top_left_x should be ranged from 0 to (large.width()-small.width())
top_left_y should be ranged from 0 to (large.height()-small.height())
*/
if(large.width()<small.width() || large.height()<small.height()) return -1;
if(top_left_x<0 || top_left_x>(large.width()-small.width()) || top_left_y<0 || top_left_y>(large.height()-small.height())) return -2;
double pixel_count = 0;
double matched_count = 0;
for(unsigned int y=0; y<small.height(); ++y)
{
for(unsigned int x=0; x<small.width(); ++x)
{
if(large.get_pixel(top_left_x+x, top_left_y+y)==small.get_pixel(x,y)) ++matched_count;
++pixel_count;
}
}
return matched_count/pixel_count;
}
bool identify_bitmap(bitmap_image& large, bitmap_image& small, POINT& result_point, double min_match_percent = 1.0)
{
if(large.width()<small.width() || large.height()<small.height()) return false;
unsigned int p_x, p_y;
double match_percent = 0;
for(unsigned int y=0; y<(large.height()-small.height()+1); ++y)
{
for(unsigned int x=0; x<(large.width()-small.width()+1); ++x)
{
double percent = compare_bitmap(large, small, x, y);
if(percent>match_percent)
{
p_x = x;
p_y = y;
match_percent = percent;
}
}
}
if(match_percent<min_match_percent) return false;
result_point.x = p_x;
result_point.y = p_y;
return true;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
string s = "capture_2.bmp";
bitmap_image image(s);
if(!image) qDebug()<<"failed";
else qDebug()<<"succeed";
}
MainWindow::~MainWindow()
{
delete ui;
}
答案 0 :(得分:0)
更新:我只是为自己找到解决方案。对不起该帖子。
解决方案:The first post to this thread声明我们可以在#define WIN32_LEAN_AND_MEAN
之前 #include <Windows.h>
虽然我不完全理解原因......