2015-09-08 42 views
0

我正在使用OpenCv中的连接组件标签(CCL)操作(使用C++语言)。要查看CCL是否可靠工作,我必须在调试时检查图像中的每个像素值。我试过将CCL的结果保存为图像,但是我无法达到像素的数字值。在调试过程中有没有办法做到这一点?OpenCv查看图像中的每个像素值

+0

你使用哪种IDE进行调试? – Gombat

+0

@Gombat VS 2013 – elmass

回答

0

将CCL矩阵转换为[0,255]范围内的值并将其保存为图像。例如:

cv::Mat ccl = ...; // ccl operation returning CV_8U 
double min, max; 
cv::minMaxLoc(ccl, &min, &max); 
cv::Mat image = ccl * (255./max); 
cv::imwrite("ccl.png", image); 

或所有值存储在一个文件中:

std::ofstream f("ccl.txt"); 
f << "row col value" << std::endl; 
for (int r = 0; r < ccl.rows; ++r) { 
    unsigned char* row = ccl.ptr<unsigned char>(r); 
    for (int c = 0; c < ccl.cols; ++c) { 
    f << r << " " << c << " " << static_cast<int>(row[c]) << std::endl; 
    } 
} 
0

当然有,但它取决于您使用的图像类型。

http://docs.opencv.org/doc/user_guide/ug_mat.html#accessing-pixel-intensity-values

你使用哪种IDE进行调试?有一个Visual Studio插件的OpenCV:

http://opencv.org/image-debugger-plug-in-for-visual-studio.html https://visualstudiogallery.msdn.microsoft.com/e682d542-7ef3-402c-b857-bbfba714f78d

要简单地打印简历::垫类型CV_8UC1到一个文本文件,使用下面的代码:

// create the image 
int rows(4), cols(3); 
cv::Mat img(rows, cols, CV_8UC1); 

// fill image 
for (int r = 0; r < rows; r++) 
{ 
    for (int c = 0; c < cols; c++) 
    { 
    img.at<unsigned char>(r, c) = std::min(rows + cols - (r + c), 255); 
    } 
} 

// write image to file 
std::ofstream out("output.txt"); 

for (int r = -1; r < rows; r++) 
{ 
    if (r == -1){ out << '\t'; } 
    else if (r >= 0){ out << r << '\t'; } 

    for (int c = -1; c < cols; c++) 
    { 
    if (r == -1 && c >= 0){ out << c << '\t'; } 
    else if (r >= 0 && c >= 0) 
    { 
     out << static_cast<int>(img.at<unsigned char>(r, c)) << '\t'; 
    } 
    } 
    out << std::endl; 
} 

只需更换IMG ,排,排列着你的变种,把“填充图像”放在一边,它应该起作用。第一行和第一列是该行/列的索引。 “output.txt”将保留在您可以在Visual Studio的项目调试设置中指定的调试工作目录中。

+0

我需要查看矩阵形式的像素值,以便我可以看到CCL是否成功。 – elmass

+1

顺便说一下,感谢Image Watch插件.. – elmass

+0

它是一个每像素值为一个值的图像吗?这些花车吗? – Gombat

0

如已经由@Gombat和例如提到here,在Visual Studio中可以安装Image Watch

如果要将Mat的值保存为文本文件,则不需要重新创建任何内容(请参阅OpenCV Mat: the basic image container)。

例如,您可以保存一个CSV文件只是想:

Mat img; 
// ... fill matrix somehow 
ofstream fs("test.csv"); 
fs << format(img, "csv"); 

完整的示例:

#include <opencv2\opencv.hpp> 
#include <iostream> 
#include <fstream> 

using namespace std; 
using namespace cv; 

int main() 
{ 
    // Just a green image 
    Mat3b img(10,5,Vec3b(0,255,0)); 

    ofstream fs("test.csv"); 
    fs << format(img, "csv"); 

    return 0; 
} 
+0

如何转换为CSV文件格式?你的代码不能在我的代码中编译。 format()函数给出了错误,网络中没有关于这个的有用示例。 @Miki – elmass

+0

@elmass我发布了一个完整的工作示例。你可以在我上面发布的链接的文档中找到它:http://docs.opencv.org/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html#output-formatting – Miki