缩放像素的图像使用opencv库和c++
scale pixels of an image using opencv library and c++
我正在尝试使用OpenCV 2.4.5和Visual Studio 2010 Express创建简单的1D条形码阅读器。
下面是我的代码://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=白色)10100 & lt; . .白色像素. .> 00101
我这样做的原因很简单…
假设我有一个UPC条形码,它可以是81像素长。然而,我加载的图像长度超过1000像素。
要应用检测并将加载的图像与UPC模式进行比较,我必须首先缩放加载的图像以纠正像素值。(我用"规模"这个词…因为如果我只是"调整"我的图像大小…它将截断919个像素,使检测不可能。)
我知道加载的图像是因子12,34到UPC模式(接近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
我认为你应该重做你的自行车。这是正确的,但是太复杂了。代码的问题在这一行:
buffer[col] = next_black_color;
变量col总是增加的,所以更新的颜色计数被添加到数组中的新槽。在您的示例中,位置5不能有97,因为给定您的代码,在位置5,您只处理了5个像素。
您的代码的另一个小问题是,您有两个相互排斥的条件。如果color <127和颜色> 127。首先,如果颜色是<127, else表示color>=127。等号很重要!如果所有颜色都是127,你的代码就会失败。
以下是算法的草稿:
int arr[] = {0,0,180,180,180,180,180,180,180,180,180,0,0,0};
int size = 14;
bool last_dark = false;
bool current_dark = false;
if(arr[0] < 127){
last_dark = true;
}
int counter = 0;
for(int i = 0; i < size; i++){
if(arr[i] < 127){
current_dark = true;
} else {
current_dark = false;
}
// is current pixel same shade as last?
if(last_dark == current_dark){
counter++;
} else {
cout << counter << endl;
counter = 1; // the last color is already processed
}
last_dark = current_dark;
}
// following line is important to get the last count
cout << counter << endl;
绝不是完整的。你必须适应自己的需要。在最后一个if中,我们不能直接比较last和current的值,因为120和12都是暗的,但不是相同的值。在代码中,用适当的向量赋值替换cout,不要忘记循环之外的cout。;)
问候,
jnovacho
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 如何将C++中的库和头与MinGW一起使用
- C++ — "算法"库和命名空间"std"
- 如何在 docker 中使用静态 opencv 库编译 c++ 应用程序
- 在 Julia 中使用 boost 库和 Windows 上的 Cxx.jl
- C++库和自注册类:客户端应用程序中的工厂映射为空
- 如果同时存在共享库和动态库,则链接器将首选哪个库?
- 如何在QApplication中应用QLibraryInfo中加载的库和插件配置
- 正确地编写一个类,并将pthread与vlc库和c++一起使用
- 编译器找不到链接的 OpenCV 库
- 如何在 CentOS 中链接 OpenCV 库
- openCV 库调用不存在的本地路径
- 如何有效地在OpenCV Mat和GpuMat上进行元素处理?
- CMAKE:查找/添加Visual Studio或Windows SDK库和标头的正确方法?
- 我最近正在阅读opencv源代码。有没有更好的方法来在opencv库中查找类定义?
- OpenCV 致命错误,同时编译带有 openmp 和 opencv 库的 cpp 文件
- 在 OpenCV 中检查库和编译
- 语法突出显示VS2012 C++和OpenCV库
- 缩放像素的图像使用opencv库和c++
- OpenCV的运行库和静态库