栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【OpenCV C++&Python】(三)改变颜色空间、图像的几何变换

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【OpenCV C++&Python】(三)改变颜色空间、图像的几何变换

文章目录
  • 改变颜色空间、图像的几何变换
    • Python
      • 改变颜色空间
      • 图像的几何变换
        • 缩放
        • 平移
        • 旋转
        • 仿射变换
        • 透视变换
    • C++
      • 改变颜色空间
      • 图像的几何变换
        • 缩放
        • 平移
        • 旋转
        • 仿射变换
        • 透视变换

改变颜色空间、图像的几何变换 Python 改变颜色空间

OpenCV有150多种颜色空间转换方法,其中常用的方法是:BGR↔ Gray和BGR↔ HSV。

对于颜色转换,我们使用函数cv.cvtColor(input_image, flag)实现,其中flag决定转换的类型。

对于BGR→ Gray转换,我们使用flag:cv.COLOR_BGR2GRAY。 BGR → HSV则使用 cv.COLOR_BGR2HSV。要获取其他flag,只需在Python终端中运行以下命令:

import cv2 as cv
flags = [i for i in dir(cv) if i.startswith('COLOR_')]
print( flags )

注意: OpenCV中,HSV的色调(H)范围为[0, 179],饱和度(S)范围为[0, 255],值(V)范围为[0, 255]。不同的软件使用不同的范围。因此,如果要将OpenCV的HSV值与它们进行比较,则需要规范化这些范围。

提取彩色对象:

在HSV表示颜色比BGR颜色空间更容易。现在我们知道了如何将BGR图像转换为HSV,我们可以使用它来提取彩色对象。下面,我们将尝试提取图中深蓝色瓶盖:

如何找到要定位颜色的HSV值?

你只需传递所需的BGR值给 cv.cvtColor()。例如,查找蓝色的HSV值:

import cv2 as cv
import numpy as np

blue = np.uint8([[[255, 0, 0]]])
hsv_blue = cv.cvtColor(blue, cv.COLOR_BGR2HSV)
print(hsv_blue)

[[[120 255 255]]]

不断调整后取[120-10, 60,60] 和[120+10, 255, 255]作为下界和上界。

image = cv.imread('cap.jpg')
#  将图像从BGR转换到HSV颜色空间
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
# 定义HSV中蓝色的下界和上界
lower_blue = np.array([110, 60, 60])
upper_blue = np.array([130, 255, 255])
# [lower_blue,upper_blue]内的像素为255,其他为0
mask = cv.inRange(hsv, lower_blue, upper_blue)
# 对mask和image按位和  提取深蓝色瓶盖
res = cv.bitwise_and(image, image, mask=mask)
cv.imshow('image', image)
cv.imshow('mask', mask)
cv.imshow('res', res)
k = cv.waitKey(0)
cv.destroyAllWindows()

图像的几何变换

OpenCV提供了两个变换函数:cv.warpAffine 和 cv.warpPerspective,使用它们可以执行各种变换。cv.warpAffine 使用2x3的变换矩阵,而 cv.warpPerspective将3x3变换矩阵作为输入。

缩放

OpenCV的cv.resize() 用于缩放图像。有多种插值方法:cv.INTER_AREA用于收缩,cv.INTER_CUBIC (slow) 和 cv.INTER_LINEAR用于放大。默认情况下,插值方法为 cv.INTER_LINEAR。可以通过指定倍数或指定大小调整图像大小的方法:

import cv2 as cv
# 指定倍数
img = cv.imread('image.jpg')
res1 = cv.resize(img, None, fx=0.5, fy=0.5, interpolation=cv.INTER_AREA)  
# 或者:指定大小
height, width = img.shape[:2]
res2 = cv.resize(img, (int(1.5 * width), int(1.5 * height)), interpolation=cv.INTER_CUBIC)  
cv.imshow('res1', res1)
cv.imshow('res2', res2)
k = cv.waitKey(0)
cv.destroyAllWindows()

平移

平移是物体位置的移动。如果知道 ( x , y ) (x, y) (x,y)方向上的偏移 ( t x , t y ) (t_x,t_y) (tx​,ty​),则可以按如下方式创建平移矩阵 M M M:
M = [ 1 0 t x 0 1 t y ] M=left[begin{array}{lll} 1 & 0 & t_{x} \ 0 & 1 & t_{y} end{array}right] M=[10​01​tx​ty​​]

你可以将 M M M放入np.float32的Numpy数组中。并将其传递到 cv.warpAffine()函数。以下给出有关(100,50)移位的示例:

import numpy as np
import cv2 as cv

img = cv.imread('idiot.jpg')
rows, cols, _ = img.shape
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv.warpAffine(img, M, (cols, rows))  
cv.imshow('img', dst)  
cv.waitKey(0)
cv.destroyAllWindows()

cv.warpAffine()的第三个参数是输出图像的大小,其形式应为 (宽度、高度) 。记住宽度=列数,高度=行数。

旋转

旋转角度为 θ θ θ的变换矩阵为
M = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] M=left[begin{array}{cc} cos theta & -sin theta \ sin theta & cos theta end{array}right] M=[cosθsinθ​−sinθcosθ​]

但OpenCV提供了具有可调旋转中心的缩放旋转,可以在任何位置 ( c e n t e r . x , c e n t e r . y ) ( { center. } x, { center. } y) (center.x,center.y)旋进行转。修改后的变换矩阵如下所示:

[ α β ( 1 − α ) ⋅ c e n t e r . x − β ⋅ c e n t e r . y − β α β ⋅ c e n t e r . x + ( 1 − α ) ⋅ c e n t e r . y ] left[begin{array}{ccc} alpha & beta & (1-alpha) cdot { center. } x-beta cdot { center. } y \ -beta & alpha & beta cdot { center. } x+(1-alpha) cdot { center. } y end{array}right] [α−β​βα​(1−α)⋅center.x−β⋅center.yβ⋅center.x+(1−α)⋅center.y​]
其中:
α = s c a l e ⋅ cos ⁡ θ β = s c a l e ⋅ sin ⁡ θ begin{array}{l} alpha= { scale } cdot cos theta \ beta= { scale } cdot sin theta end{array} α=scale⋅cosθβ=scale⋅sinθ​

OpenCV提供了一个函数 cv.getRotationMatrix2D来得到这个变换矩阵。下面的示例将图像相对于中心旋转90度( θ = 90 theta=90 θ=90)而不进行任何缩放( s c a l e = 1 scale=1 scale=1)。

import cv2 as cv
img = cv.imread('image.jpg')
rows, cols, _ = img.shape
# (旋转的中心点x,旋转的中心点y,旋转的角度,图像缩放因子)
M = cv.getRotationMatrix2D(((cols - 1) / 2.0, (rows - 1) / 2.0), 90, 1)
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('img', dst)
cv.waitKey(0)
cv.destroyAllWindows()

仿射变换

在仿射变换中,原始图像中的所有平行线在输出图像中仍然是平行的。为了找到变换矩阵,我们需要从输入图像中选取三个点并指定其在输出图像中的相应位置,然后传递给cv.getAffineTransform创建一个2x3矩阵,最后该该矩阵将传递cv.warpAffine进行变换。

import cv2 as cv
import numpy as np

img = cv.imread('affine.jpg')
rows, cols, ch = img.shape
img[45:55, 45:55, :] = [255, 0, 0]
img[195:205, 45:55, :] = [0, 255, 0]
img[45:55, 195:205, :] = [0, 0, 255]
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
cv.imshow('img', img)
cv.imshow('dst', dst)
cv.waitKey(0)



透视变换

对于透视变换,需要输入图像上的4个点和输出图像上的相应点来得到变换矩阵,且在这4个点中,有3个不应共线。在OpenCV中可以使用 cv.getPerspectiveTransform求出该变换矩阵,然后使用cv.warpPerspective应用这个变换矩阵。

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread('perspective.jpg')

rows, cols, ch = img.shape
pts1 = np.float32([[290, 139], [362, 121], [331, 209], [409, 191]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
M = cv.getPerspectiveTransform(pts1, pts2)
dst = cv.warpPerspective(img, M, (300, 300))  //

cv.imshow('img', img)
cv.imshow('dst', dst)
cv.waitKey(0)


C++ 改变颜色空间
#include 
using namespace cv;
int main()
{
	Mat image = imread("cap.jpg");
	// 将图像从BGR转换到HSV颜色空间
	Mat hsv, mask, res;
	cvtColor(image, hsv, COLOR_BGR2HSV);
	// [Scalar(110, 60, 60), Scalar(130, 255, 255)]内的像素为255,其他为0
	inRange(hsv, Scalar(110, 60, 60), Scalar(130, 255, 255),mask);
	// 对mask和image按位和  提取深蓝色瓶盖
	bitwise_and(image, image, res,mask);
	imshow("image", image);
	imshow("mask", mask);
	imshow("res", res);
	waitKey(0);
	return 0;
}
图像的几何变换 缩放
#include 
using namespace cv;
int main()
{
	Mat img = imread("image.jpg");
	Mat res1, res2;
	// 指定倍数
	resize(img,res1, Size(),0.5,0.5,INTER_AREA);
	// 指定大小
	int height = img.rows* 1.5;
	int width = img.cols* 1.5;
	resize(img, res2, Size(width,height), 0, 0, INTER_CUBIC);
	imshow("res1", res1);
	imshow("res2", res2);
	waitKey(0);
	return 0;
}
平移
#include 
using namespace cv;
int main()
{
	Mat img = imread("image.jpg");
	Mat dst;

	//定义平移矩阵
	Mat M = cv::Mat::zeros(2, 3, CV_32FC1);
	M.at(0, 0) = 1;
	M.at(0, 2) = 100; //水平平移量
	M.at(1, 1) = 1;
	M.at(1, 2) = 50; //竖直平移量
	warpAffine(img, dst, M, Size(img.cols,img.rows));
	imshow("dst", dst);
	waitKey(0);
	return 0;
}
旋转
#include 
using namespace cv;
int main()
{
	Mat img = imread("image.jpg");
	Mat dst;

	//定义平移矩阵
	Mat M = getRotationMatrix2D(Point((img.cols - 1) / 2, (img.rows - 1) / 2), 90, 1);
	warpAffine(img, dst, M, Size(img.cols,img.rows));
	imshow("dst", dst);
	waitKey(0);
	return 0;
}
仿射变换
#include 
using namespace cv;
int main()
{
	Mat img = imread("affine.jpg");
	Mat dst;
	Mat roi1 = img(Range(45, 55), Range(45, 55));
	Mat roi2 = img(Range(195, 205), Range(45, 55));
	Mat roi3 = img(Range(45, 55), Range(195, 205));
	roi1 = Scalar(255, 0, 0);
	roi2 = Scalar(0, 255, 0);
	roi3 = Scalar(0, 0, 255);
	Mat pts1 = (Mat_(3, 2) << 50, 50, 200, 50, 50, 200);
	Mat pts2 = (Mat_(3, 2) << 10, 100, 200, 50, 100, 250);
	Mat M = getAffineTransform(pts1, pts2);
	warpAffine(img, dst, M, Size(img.cols, img.rows));
	imshow("img", img);
	imshow("dst", dst);
	waitKey(0);
	return 0;
}
透视变换
#include 
using namespace cv;
int main()
{
	Mat img = imread("perspective.jpg");
	Mat dst;
	Mat pts1 = (Mat_(4, 2) << 290, 139, 362, 121, 331, 209, 409, 191);
	Mat pts2 = (Mat_(4, 2) << 0, 0, 300, 0, 0, 300, 300, 300);
	Mat M = getPerspectiveTransform(pts1, pts2);
	warpPerspective(img, dst, M, Size(300, 300));
	imshow("img", img);
	imshow("dst", dst);
	waitKey(0);
	return 0;
}

代码:

https://gitee.com/BinaryAI/open-cv-c–and-python

参考:

[1] https://docs.opencv.org/4.5.5/index.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号