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; 
}

效果:
这里写图片描述

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南山二毛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值