[英]OpenCV Mat::data is zero after image operation
我在OpenCV C ++中有問題。 我從文件中讀取圖像,並使用閾值功能獲取二進制圖像。 之后,我要訪問閾值圖像的像素值。
問題:閾值圖像的Mat :: data數組為0。
我使用閾值(src,img,220,255,CV_THRESH_BINARY); 與src和img beig cv :: Mat。
之后,在img :: data =“”中調用
這是我的代碼:
// read image source
Mat src = imread("image.png", CV_LOAD_IMAGE_COLOR);
if(src.empty())
{
return -1;
}
cvtColor(src,src,CV_RGB2GRAY);
threshold(src, src, 220, 255, CV_THRESH_BINARY );
int line[1200];
int lineCount = 0;
for(int i=src.rows; i>src.rows/2; i-=10)
{
uchar* rowi = src.ptr(i-1);
int columnCount = 0;
// begin on the left of the image
for(int j=0; j<src.cols; j++)
{
uchar grayscale = src.at<uchar>(rowi[j]);
// write data to array
line[columnCount] = grayscale;
columnCount++;
}
}
我認為問題可能與引用和復制Mat :: data的cv :: Mat有關:
謝謝你的幫助!
我想您已經檢查過image.png(轉換為灰度后)包含的值大於220嗎?
您可能想嘗試將cvtColor
作為非就地操作執行,因為它可能無法正確支持就地轉換(請參閱https://stackoverflow.com/a/15345586/19254 )。 例如,您可以這樣做:
Mat dst;
cvtColor(src, dst, CV_RGB2GRAY);
...然后在dst
threshold
。 或者,您可以從灰度模式開始將圖像讀取到內存中(要imread
第二個參數可以是CV_LOAD_IMAGE_GRAYSCALE
),在這種情況下, cvtColor
調用。
每次調用之間,你可以imwrite
圖像到磁盤,以找出哪些步驟無法按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.