我正嘗試使用OpenCV 2.4.5和Visual Studio 2010 Express創建簡單的1D條形碼讀取器。使用opencv庫和C++的圖像縮放像素
這裏是到目前爲止我的代碼:
//define Image path:
char* imageName = "D:\\Barcode Reader\\test3.jpg";
cv::Mat src = cv::imread(imageName);
if(!src.data)
{ return -1; }
//convert image to grayscale img:
cv::Mat gray_image;
cvtColor (src, gray_image, CV_BGR2GRAY);
unsigned char color;
unsigned char next_black_color = 0;
unsigned char next_white_color = 0;
int buffer[500];
float factor = (float)gray_image.cols/(float)required_width;
//start to search for pixels from left to right (in the middle of the img):
unsigned char *position = gray_image.ptr(gray_image.rows/2,0);
//iterate through the whole image length:
for (int col = 1; col <= gray_image.cols; col++)
{
//...and store the pixel value in color variable for possible output (which would be like 0-127 for black colors and 127-255 for white colors:
color = *position;
position++;
//check the pixel value (< 127 everything bellow has dark color):
if (color < 127)
{
//...and after each position checked, go to next pixel and save the number of occurences of black pixels:
next_black_color++;
buffer[col] = next_black_color;
std::cout << col << ": " << buffer[col] << " ";
}
else
{
//set the counter variable to null for the next occurence of black pixel:
next_black_color = 0;
}
//the same goes for white pixels:
if (color > 127)
{
next_white_color++;
buffer[col] = next_white_color;
std::cout << col << ": " << buffer[col] << " ";
}
else
{
next_white_color = 0;
}
}
//show the results:
std::cout<<" Number of pixels in width = " << src.cols << std::endl <<
"Number of pixels in height = " << src.rows << std::endl;
cv::imshow("Normal Image", src);
cv::imshow("Gray Image", gray_image);
cv::waitKey(0);
return 0;
測試圖像是在按下列順序 與黑色和白色像素100x100px圖片(描述爲二進制碼用於更好地理解:1 =黑,0 =白) ..white像素..> 00101
我做這個很簡單的原因...
比方說,我有一個UPC條碼可以是81個像素長。 但是,我加載的圖片長度超過1000像素。
要應用檢測,並將我加載的圖像與UPC圖案進行比較,我必須首先縮放加載的圖像以正確校正像素值。 (我用的是「規模」 ......因爲如果我只是「調整」我的形象......這將切斷919個像素,使得檢測是不可能的。)
我知道加載的圖像是UPC模式的因素12,34(接近12 ....我現在不關心正確的值...我所關心的是目前的實施...)
- 所以要實現縮放,我必須計算每個黑白像素的出現次數,將其保存在一個數組中,然後用我的因子除以得到縮放值。
我使用這個實施面臨着以下問題:
的出現次數將被存儲爲以下幾點:
____[Array]____
Position | Occurence
1 ......... 1 (First position with first black pixel)
2 ......... 1 (white)
3 ......... 1 (black)
4 ......... 1 (white pixels until next black pixel appears..)
5 ......... 2 (___Problem here!___ ... should be 94!)
6 ......... 3 .
. ......... . .
100 ....... 100(end)
但它應該是:
____[Array]____
Position | Occurence
1 ......... 1 (First position with first black pixel)
2 ......... 1 (white)
3 ......... 1 (black)
4 ......... 94 (count all white pixels until black appears again)
5 ......... 1 (black)
6 ......... 1 (white)
7 ......... 1 (black) -> end
我希望我能提供足夠的答案信息。
請幫我解決我的問題。 祝你好運 Ondrej
首先使用imread()從highgui模塊,cvLoadImage爲C API。 第二個你說的,那就是你的測試圖像中的0 - 黑色,1 - 白色。但條件是測試<127。 0,1都小於127,因此不起作用。將白色像素設置爲255(例如,使用閾值)或將條件設置爲正確的值。 – jnovacho
已編輯的文章,一些單詞用更準確的含義替換,用cv :: imread代替cvLoadImage。感謝您的更正。我正在使用cvLoadImage,因爲cv :: imread不能以調試模式運行。 – Ondrej
imread如何運行?有沒有可能,你沒有用調試符號編譯highgui? – jnovacho