下面两版代码,第一个代码运行时灰度值不能随滚动条的调节而变化,但是第二版程序就可以。麻烦帮我分析一下为什么?环境是vs2012+opencv2.4.8
#include #include #include #include using namespace std; using namespace cv; static void on_thresholdAdjustment(int, void*); //滚动条回调函数 int g_threshold=70; Mat binaryImage; Mat dstImage; int main() { //1.读取图像并在窗口中显示 Mat srcImage = imread("dota.jpg", 1); if (!srcImage.data) { cerr <<"图片读取错误!\n"; return -1; } namedWindow("原图窗口"); imshow("原图窗口", srcImage); waitKey(0); //2.对灰度图像进行二值化处理 //srcImage.copyTo(binaryImage); binaryImage = srcImage.clone(); if (!binaryImage.data) { cerr << "binaryImage is empty!\n" <(i); //获取第i行的首地址 for(int j = 0; j < colNumber; j++) { if (data[j] < (g_threshold)) { data[j] = 0; } else { data[j] = 255; } } } imshow("二值化图像", binaryImage); cout<< "finished" << endl; }
第二版程序 使用了一个中间的mat
#include #include #include #include using namespace std; using namespace cv; static void on_thresholdAdjustment(int, void*); //滚动条回调函数 int g_threshold=70; Mat binaryImage; Mat dstImage; int main() { //1.读取图像并在窗口中显示 Mat srcImage = imread("dota.jpg", 1); if (!srcImage.data) { cerr <<"图片读取错误!\n"; return -1; } namedWindow("原图窗口"); imshow("原图窗口", srcImage); waitKey(0); //2.对灰度图像进行二值化处理 //srcImage.copyTo(binaryImage); binaryImage = srcImage.clone(); if (!binaryImage.data) { cerr << "binaryImage is empty!\n" <(i); //获取第i行的首地址 uchar* data2 = dstImage.ptr(i); //这里不同 for(int j = 0; j < colNumber; j++) { if (data[j] < (g_threshold)) { data2[j] = 0; } else { data2[j] = 255; } } } imshow("二值化图像", dstImage); cout<< "finished" << endl; }
I want to tell you that the first program is not ineffective, but the status of the first program has been saved in binaryImage
After running the on_thresholdAdjustment function for the first time, the content in binaryImage has only There are two values: 0 and 255
Modifications in the future will be based on the only 0 and 255 in binaryImage, so the threshold g_threshold will inevitably fail. You can see the effect by changing the threshold to 0.
It is very necessary to keep the original information. You cannot always plan based on the calculated values. You must use the original information for calculation. If you don’t want 3 Mats, you can change it like this: