OpenCV学习5--图像操作

修改像素值

灰度图像

img.at< uchar>(y,x) = 128;


RGB三通道图像

img.at< Vec3b>(y,x)[0]=128;//blue

img.at< Vec3b>(y,x)[1]=128;//blue

img.at< Vec3b>(y,x)[2]=128;//blue


空白图像

img=Scalar(0);


ROI选择

Rect r(10,10,100,100);

Mat smalling=img(r);


Vec3b与Vec3F

Vec3b对应三通道的顺序是blue、green、red的uchar类型数据

Vec3f对应三通道的float类型数据

把CV_8UC1转换到CV32F1实现如下:

src.convertTo(dst,CV_32F);


实例程序:

#include<iostream>
#include<opencv2/core/core.hpp>
#include<highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>  

using namespace cv;
using namespace std;

int main()
{ 
    Mat img = imread("1.jpg");
    if(img.empty())
    {
        cout << "Image Load Failed"<< endl;
        //system("pause");
        return -1;
    }

    namedWindow("MyWindow2", CV_WINDOW_AUTOSIZE);
    imshow("MyWindow2", img);
    Mat gray_img;
    cvtColor(img,gray_img,CV_BGR2GRAY);
    int height = gray_img.rows;
    int width = gray_img.cols;

    namedWindow("output",WINDOW_AUTOSIZE);
    imshow("output",gray_img);

    //单通道的像素操作
    for(int row = 0;row<height;row++)
    {
        for(int col = 0;col<width;col++)
        {
            int gray = gray_img.at<uchar>(row,col);
            gray_img.at<uchar>(row,col) = 255 -gray;
        }
    }

    imshow("output2",gray_img);

    Mat dst;
    dst.create(img.size(),img.type());
    height = dst.rows;
    width = dst.cols;
    int cn = dst.channels();

    for(int row = 0;row<height;row++){
        for(int col = 0;col < width;col++){
            if(cn == 1)
            { 
            }
            else if(cn == 3){                          //彩色图像的反差处理
                int b = img.at<Vec3b>(row,col)[0]; 
                int g = img.at<Vec3b>(row,col)[1]; 
                int r = img.at<Vec3b>(row,col)[2]; 
                dst.at<Vec3b>(row,col)[0] = 255 -b;
                dst.at<Vec3b>(row,col)[1] = 255 -g;
                dst.at<Vec3b>(row,col)[2] = 255 -r;
            }
        }
    }

    imshow("output4",dst);
    Mat outimg;
    bitwise_not(img,outimg);
    imshow("output5",outimg);
    waitKey(0);

    return 0; 
}


效果:

image.png

源码和原图片请到Github下载:
https://github.com/MRwangmaomao/OpencvPixel-Project.git




————————————————

版权声明:本文为CSDN博主「南山二毛」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_16481211/article/details/79561262


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864