2015-09-08 46 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