C++使用opencv实现彩色直方图计算

在学习opencv的时候,跑opencv官网例程的源码出现了一个问题:

0x000007FEE09A532C (opencv_world310.dll) (XXX.exe 中)处有未经处理的异常:  0xC0000005:  读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。

查了很久才发现原来是库的问题!!源码如下:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include 
#include 

using namespace std;
using namespace cv;
int main()
{
	Mat src, dst;
	src = imread("photo.jpg");
	if (src.empty())
	{
		return -1;
	}
	vector bgr_planes;
	split(src, bgr_planes);
	int histSize = 256;
	float range[] = { 0, 256 };
	const float* histRange = { range };
	bool uniform = true; bool accumulate = false;
	Mat b_hist, g_hist, r_hist;
	calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
	calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
	calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);
	// Draw the histograms for B, G and R
	int hist_w = 512; int hist_h = 400;
	int bin_w = cvRound((double)hist_w / histSize);
	Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
	normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
	normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
	normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
	for (int i = 1; i < histSize; i++)
	{
		line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(b_hist.at(i - 1))),
			Point(bin_w*(i), hist_h - cvRound(b_hist.at(i))),
			Scalar(255, 0, 0), 2, 8, 0);
		line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(g_hist.at(i - 1))),
			Point(bin_w*(i), hist_h - cvRound(g_hist.at(i))),
			Scalar(0, 255, 0), 2, 8, 0);
		line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at(i - 1))),
			Point(bin_w*(i), hist_h - cvRound(r_hist.at(i))),
			Scalar(0, 0, 255), 2, 8, 0);
	}
	namedWindow("calcHist Demo", WINDOW_AUTOSIZE);
	imshow("calcHist Demo", histImage);
	waitKey(0);
	return 0;
}


其实问题主要就是出在了 opencv_world310.dll这个地方,因为我是在debug模式下运行的,这个库是release下的库,因此引用了之后是有问题的。当时我配置的时候是两个路径都加在里面,如图:

C++使用opencv实现彩色直方图计算_第1张图片


出错的时候直接用的第一个opencv_world310.lib,而没有去用第二个带d的。于是我把第一删了,只留了第二个。在debug模式下运行,问题终于解决,成功输出直方图。

 

你可能感兴趣的:(C++使用opencv实现彩色直方图计算)