当先锋百科网

首页 1 2 3 4 5 6 7

利用opencv做一些计算机视觉的操作。实现的功能就是将彩色图片变成灰白的,并对灰度图片作边缘化提取操作。下图展示的是灰度图和边缘图。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;

int main()
{
	Mat img_rgb, img_gry, img_cny;

	namedWindow("Example Gray",WINDOW_AUTOSIZE);
	namedWindow("Example Canny",WINDOW_AUTOSIZE);

	img_rgb = imread("H:\\vs2017\\opencv_learning\\ConsoleApplication1\\img.jpg");
	
	cvtColor(img_rgb,img_gry,COLOR_BGR2GRAY);
	imshow("Example Gray",img_gry);

	Canny(img_gry,img_cny,10,100,3,true);
	imshow("Example Canny",img_cny);



	waitKey(0);

	return 0;

}


对图片的某一个像素进行读取,并修改该像素值。下边代码展示的是对衣服全灰的图,在某一个像素位置使其变成一个小白点。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;

int main()
{
	Mat img_rgb, img_gry, img_cny;

	namedWindow("Example Gray",WINDOW_AUTOSIZE);
	namedWindow("Example Canny",WINDOW_AUTOSIZE);

	img_rgb = imread("H:\\vs2017\\opencv_learning\\ConsoleApplication1\\img.png");
	
	cvtColor(img_rgb,img_gry,COLOR_BGR2GRAY);
	int x = 16, y = 32;
	Vec3b intensity = img_rgb.at< Vec3b >(y, x);
	// ( Note: We could write img_rgb.at< cv::Vec3b >(x,y)[0] )
	//
	uchar blue = intensity[0];
	uchar green = intensity[1];
	uchar red = intensity[2];

	std::cout << "At (x,y) = (" << x << ", " << y <<
		"): (blue, green, red) = (" <<
		(unsigned int)blue <<
		", " << (unsigned int)green << ", " <<
		(unsigned int)red << ")" << std::endl;


	std::cout << "Gray pixel there is: " <<
		(unsigned int)img_gry.at<uchar>(y, x) << std::endl;

	img_gry.at<uchar>(x, y) = 255; // Set the gray pixel

	imshow("Example Gray",img_gry);

	Canny(img_gry,img_cny,10,100,3,true);
	imshow("Example Canny",img_cny);

	waitKey(0);

	getchar();
	return 0;

}