使用Opencv_CUDA 实现访问图像像素、直方图均衡化、几何变换
访问图像的各个像素强度 直方图的计算与均衡 几何变换:缩放、平移与旋转
1. 访问图像的各个像素强度
# include <iostream>
# include "opencv2/opencv.hpp"
int main ( )
{
cv:: Mat h_img1 = cv:: imread ( "images/cameraman.tif" , 0 ) ;
cv:: Scalar intensity = h_img1. at < uchar> ( cv:: Point ( 100 , 50 ) ) ;
std:: cout << "Pixel Intensity of gray scale Image at (100,50) is:" << intensity. val[ 0 ] << std:: endl;
cv:: Mat h_img2 = cv:: imread ( "images/autumn.tif" , 1 ) ;
cv:: Vec3b intensity1 = h_img2. at < cv:: Vec3b> ( cv:: Point ( 100 , 50 ) ) ;
std:: cout << "Pixel Intensity of color Image at (100,50) is:" << intensity1 << std:: endl;
return 0 ;
}
2. 直方图计算与均衡
直方图时图像的一个非常重要的属性,提供了图像外观的全局描述 通常直方图均匀分布,可以得到较好的视觉效果 对灰度图实现均衡化:
# include <iostream>
# include "opencv2/opencv.hpp"
# include <cudaimgproc.hpp>
int main ( )
{
cv:: Mat h_img1 = cv:: imread ( "images/cameraman.tif" , 0 ) ;
cv:: cuda:: GpuMat d_img1, d_result1;
d_img1. upload ( h_img1) ;
cv:: cuda:: equalizeHist ( d_img1, d_result1) ;
cv:: Mat h_result1;
d_result1. download ( h_result1) ;
cv:: imshow ( "Original Image " , h_img1) ;
cv:: imshow ( "Histogram Equalized Image" , h_result1) ;
cv:: imwrite ( "images/result_inversion.png" , h_img1) ;
cv:: imwrite ( "images/result_inversion.png" , h_result1) ;
cv:: waitKey ( ) ;
return 0 ;
}
# include <iostream>
# include "opencv2/opencv.hpp"
int main ( )
{
cv:: Mat h_img1 = cv:: imread ( "images/autumn.tif" ) ;
cv:: Mat h_img2, h_result1;
cvtColor ( h_img1, h_img2, cv:: COLOR_BGR2HSV) ;
std:: vector< cv:: Mat > vec_channels;
cv:: split ( h_img2, vec_channels) ;
cv:: equalizeHist ( vec_channels[ 2 ] , vec_channels[ 2 ] ) ;
cv:: merge ( vec_channels, h_img2) ;
cv:: cvtColor ( h_img2, h_result1, cv:: COLOR_HSV2BGR) ;
cv:: imshow ( "Original Image " , h_img1) ;
cv:: imshow ( "Histogram Equalized Image" , h_result1) ;
cv:: waitKey ( ) ;
return 0 ;
}
3. 几何变换
# include <iostream>
# include "opencv2/opencv.hpp"
# include <opencv2/cudawarping.hpp>
int main ( )
{
cv:: Mat h_img1 = cv:: imread ( "images/cameraman.tif" , 0 ) ;
cv:: cuda:: GpuMat d_img1, d_result1, d_result2;
d_img1. upload ( h_img1) ;
int width = d_img1. cols;
int height = d_img1. size ( ) . height;
cv:: cuda:: resize ( d_img1, d_result1, cv:: Size ( 200 , 200 ) , cv:: INTER_CUBIC) ;
cv:: cuda:: resize ( d_img1, d_result2, cv:: Size ( 0.5 * width, 0.5 * height) , cv:: INTER_LINEAR) ;
cv:: Mat h_result1, h_result2;
d_result1. download ( h_result1) ;
d_result2. download ( h_result2) ;
cv:: imshow ( "Original Image " , h_img1) ;
cv:: imshow ( "Resized Image" , h_result1) ;
cv:: imshow ( "Resized Image 2" , h_result2) ;
cv:: imwrite ( "Resized1.png" , h_result1) ;
cv:: imwrite ( "Resized2.png" , h_result2) ;
cv:: waitKey ( ) ;
return 0 ;
}
# include <iostream>
# include "opencv2/opencv.hpp"
# include <opencv2/cudawarping.hpp>
int main ( )
{
cv:: Mat h_img1 = cv:: imread ( "images/cameraman.tif" , 0 ) ;
cv:: cuda:: GpuMat d_img1, d_result1, d_result2;
d_img1. upload ( h_img1) ;
int cols = d_img1. cols;
int rows = d_img1. size ( ) . height;
cv:: Mat trans_mat = ( cv:: Mat_ < double > ( 2 , 3 ) << 1 , 0 , 70 , 0 , 1 , 50 ) ;
cv:: cuda:: warpAffine ( d_img1, d_result1, trans_mat, d_img1. size ( ) ) ;
cv:: Point2f pt ( d_img1. cols / 2. , d_img1. rows / 2. ) ;
cv:: Mat r = cv:: getRotationMatrix2D ( pt, 45 , 1.0 ) ;
cv:: cuda:: warpAffine ( d_img1, d_result2, r, cv:: Size ( d_img1. cols, d_img1. rows) ) ;
cv:: Mat h_result1, h_result2;
d_result1. download ( h_result1) ;
d_result2. download ( h_result2) ;
cv:: imshow ( "Original Image " , h_img1) ;
cv:: imshow ( "Translated Image" , h_result1) ;
cv:: imshow ( "Rotated Image" , h_result2) ;
cv:: imwrite ( "Translated.png" , h_result1) ;
cv:: imwrite ( "Rotated.png" , h_result2) ;
cv:: waitKey ( ) ;
return 0 ;
}