Opencv -- 10画矩形及图像像素的逻辑操作

1、关于 rectangle 函数的使用说明。

rectangle 函数用于绘制矩形轮廓或填充矩形。

rectangle 函数有两个,

void rectangle(InputOutputArray img, Point pt1, Point pt2,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);

void rectangle(InputOutputArray img, Rect rec,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);

可以看出只是生成矩形的方式不同,一个是需要提供顶点坐标和对角坐标,一个需要是提供Rect函数。本例子中使用的是 point(x,y) 函数分别指定顶点和对角坐标的方式,所以这里说一下Rect函数。

Rect()是Rect类的默认构造函数,Rect(x, _Tp _y, _Tp _width, _Tp _height);是Rect类的有参构造函数。

 Rect(x, _Tp _y, _Tp _width, _Tp _height);

该函数有四个参数,x、y 代表矩形的顶点坐标,width 和 height 则代表该矩形的宽度和高度。

2、关于尝试直接使用二进制值(类似"0xFFFF00") 给函数中的代表颜色的参数赋值。

rectangle(temp1,Point(20,20), Point(70, 70),0x33/*Scalar(255,0,0)*/,-1,LINE_8,0);

以上面代码为例,代码可以运行,因为0x33的十进制是51,小于255,所以会被显示为浅蓝色(这种赋值方式只给单个通道赋值,且不能超过255,0-255为蓝色),当大于255时,会被截断到255。

因此,我们在给代表颜色的参数赋值时,使用标量函数Scalar(G,B,R)。

openCV的图像像素的逻辑操作有与、或、非、异或,但没有同或。

生成矩形时的坐标系是怎样的呢?
LCD坐标系,即窗体的左上角的坐标为(0,0)。

10_opencv_mat.h

#ifndef _10_OPENCV_MAT_H
#define _10_OPENCV_MAT_H

#include <opencv2/opencv.hpp>

using namespace cv;

class QuickDemo {
public:
	
	void bitwise_demo(Mat& image);
};

#endif

10_opencv_mat.cpp


#include <iostream>
#include "10_opencv_mat.h"

using namespace std;

void QuickDemo::bitwise_demo(Mat &image)
{
	Mat temp1 = Mat::zeros(Size(255, 255), CV_8UC3);
	Mat temp2 = Mat::zeros(Size(255, 255), CV_8UC3);
	/*
		函数cv::rectangle用于绘制矩形轮廓或填充矩形

		void rectangle(InputOutputArray img, Point pt1, Point pt2,
						  const Scalar& color, int thickness = 1,
						  int lineType = LINE_8, int shift = 0);

		@param img Image.
		输入图像
		@param pt1 Vertex of the rectangle. 
		pt1是矩形的顶点
		@param pt2 Vertex of the rectangle opposite to pt1 .
		pt2是与pt1是相对的矩形顶点
		@param color Rectangle color or brightness (grayscale image).
		矩形的填充颜色或灰度值
		@param thickness Thickness of lines that make up the rectangle. Negative values, like #FILLED (也就是 -1),
		mean that the function has to draw a filled rectangle.
		构成矩形的线条的粗细。负值,如#FILLED,意味着函数必须绘制一个填充的矩形。
		@param lineType Type of the line. See #LineTypes
		线的种类,看 LineTypes
		@param shift Number of fractional bits in the point coordinates.
		shift - 点坐标的小数位数。
	 */				
	rectangle(temp1,Point(20,20), Point(70, 70),Scalar(255,0,0),-1,LINE_8,0);//蓝
	rectangle(temp2,Point(50,50), Point(100,100), Scalar(0, 0, 255), -1, LINE_8, 0);//红
	Mat dst_and, dst_or, dst_not,dst_xor;
	bitwise_and(temp1, temp2, dst_and);
	bitwise_or(temp1, temp2, dst_or);
	//bitwise_not(image, dst_not);
	dst_not = ~image;//取反,与bitwise_not功能一致
	bitwise_xor(temp1, temp2, dst_xor);

	imshow("temp1", temp1);
	imshow("temp2", temp2);
	imshow("与操作", dst_and);
	imshow("或操作", dst_or);
	imshow("取反操作", dst_not);
	imshow("异或操作", dst_xor);
}

main.c

#include <iostream>
#include "10_opencv_mat.h"

using namespace std;

int main(int argc,char** argv)
{
    //使用Mat(matrix -- 矩阵)这种类型来读取通过指定路径的图像信息并存放到变量picture中。
    //图像从本质上来说都是二维的数组(矩阵)
    Mat picture = imread("兔子.jpeg", IMREAD_COLOR);

    if (picture.empty())
    {
        cout << "could not Load image." << endl;
        system("pause");
        return -1;
    }
    
    // namedWindow("输入窗口",WINDOW_FREERATIO);
   
    imshow("输入窗口", picture);

    QuickDemo qd;
    qd.bitwise_demo(picture);

    waitKey(0);
    destroyAllWindows();

    system("pause");
    return 0;
}

程序运行效果如下:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xuechanba

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

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

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

打赏作者

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

抵扣说明:

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

余额充值