OpenCV访问图像中的像素需要先—— 行,后—— 列

     通过OpenCV访问图像中的像素时,若先访问列再访问行的话,会出现如下错误:

OpenCV(3.4.2) Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv
::Mat::at, file d:\diyprogram\buildopencvvs\install\include\opencv2\core\mat.inl
.hpp, line 1098
OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(3.4.2) Error: Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv
::Mat::at, file d:\diyprogram\buildopencvvs\install\include\opencv2\core\mat.inl
.hpp, line 1098

 

若改为先行后列则可正确运行

代码如下:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

//OpenCV头文件
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

int main( int argc, const char** argv )
{
    //读图像
    Mat color = imread("1.jpg");
    Mat gray = imread("1.jpg", 0);

    //写图像
    imwrite("1Gray.jpg", gray);

    //通过OpenCV函数获取相同像素
    int myRow = color.rows-1;       //访问图像像素是需要先访问行再访问列
    int myCol = color.cols-1;
    Vec3b pixel = color.at<Vec3b>(myRow, myCol);
    cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," <<
            (int)pixel[1] << (int)pixel[2] << ")" << endl;

    //显示图像
    namedWindow("Lena BGR",WINDOW_NORMAL);
    namedWindow("Lena Gray",WINDOW_NORMAL);
    imshow("Lena BGR", color);
    imshow("Lena Gray", gray);

    //等待按键
    waitKey(0);
    return 0;
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

//OpenCV头文件
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

int main( int argc, const char** argv )
{
    //读图像
    Mat color = imread("1.jpg");
    Mat gray = imread("1.jpg", 0);

    //写图像
    imwrite("1Gray.jpg", gray);

    //通过OpenCV函数获取相同像素
    int myRow = color.rows-1;       //访问图像像素是需要先访问行再访问列
    int myCol = color.cols-1;
    Vec3b pixel = color.at<Vec3b>(myRow, myCol);
    cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," <<
            (int)pixel[1] << (int)pixel[2] << ")" << endl;

    //显示图像
    namedWindow("Lena BGR",WINDOW_NORMAL);
    namedWindow("Lena Gray",WINDOW_NORMAL);
    imshow("Lena BGR", color);
    imshow("Lena Gray", gray);

    //等待按键
    waitKey(0);
    return 0;
}

 

使用OpenCV库可以很容易地实现车牌图像的阈值化操作,获得二值化图。 以下是基本的代码实现: ```python import cv2 # 读取车牌图像 img = cv2.imread('plate.jpg') # 转换为灰度图像 gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 自适应二值化处理 thresh_img = cv2.adaptiveThreshold(gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) # 显示二值化图像 cv2.imshow("Thresholded Image", thresh_img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上述代码,使用了OpenCV自适应阈值处理函数 `cv2.adaptiveThreshold()`,该函数会根据图像的局部像素特征来自动调整二值化阈值,从而得到更好的二值化效果。其,`gray_img` 是原始车牌图像的灰度图像,`thresh_img` 是通过自适应阈值处理得到的二值化图像。 在调用 `cv2.adaptiveThreshold()` 函数时,需要指定以下几个参数: - `gray_img`:输入的灰度图像。 - `255`:输出的二值化图像像素值的最大值。 - `cv2.ADAPTIVE_THRESH_GAUSSIAN_C`:自适应阈值处理方法,这里采用的是高斯加权平均法。 - `cv2.THRESH_BINARY`:二值化类型,这里采用的是简单的二值化。 - `11`:像素邻域大小,表示当前像素的阈值会根据周围 11×11 个像素的灰度值自适应地调整。 - `2`:常数 C,表示在计算当前像素阈值时需要加上的常数。 最终得到的二值化图像可以通过 `cv2.imshow()` 函数进显示,其 `"Thresholded Image"` 是窗口的名称,可以根据需要修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值