图像读入:
#include
#include
using namespace cv;
int main(int argc, char** argv) {
Mat src = imread("D:/图片资源/1.png");
if(src.empty()){
printf("could not load image...\n");
return -1;
}
namedWindow("test opencv setup", CV_WINDOW_AUTOSIZE);
imshow("test opencv setup", src);
waitKey(0);
return 0;
}
直接加载原图灰度图像可以直接加参数:
Mat src = imread("D:/图片资源/1.png",IMREAD_GRAYSCALE);
保存图片:
imwrite("D:/图片资源/2.png",output_image);
图像转换与保存:
#include
#include
#include
using namespace cv;
int main(int argc, char** argv) {
Mat src = imread("D:/图片资源/1.png",IMREAD_GRAYSCALE);
if(src.empty()){
printf("could not load image...\n");
return -1;
}
namedWindow("test opencv setup", CV_WINDOW_AUTOSIZE);
imshow("test opencv setup", src);
namedWindow("output windows",CV_WINDOW_AUTOSIZE);
Mat output_image;
cvtColor(src, output_image, CV_BGR2HSV);//转换
imshow("output windows",output_image);//显示转换后图像
imwrite("D:/图片资源/2.png",output_image);
waitKey(0);
return 0;
}
掩膜操作:
#include
#include
#include
using namespace cv;
int main(int argc, char** argv) {
Mat src, dst;
src = imread("D:/图片资源/1.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image",src);
//获取图像宽度和高度,RGB图像有三个通道值
int cols = (src.cols-1) * src.channels();//宽度=图像列*通道数
int offsetx = src.channels();//图像通道
int rows = src.rows; //高度=图像高度
dst = Mat::zeros(src.size(), src.type());//初始化
for (int row = 1; row < (rows - 1); row++){
const uchar* previous = src.ptr(row - 1);//获前一行指针
const uchar* current = src.ptr(row);//获取当前行指针
const uchar* next = src.ptr(row + 1);//获取下一行指针
uchar* output = dst.ptr(row);
for (int col = offsetx; col < cols; col++) {
output[col] = saturate_cast(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
//加入函数saturate_cast();来限制范围
}
}
namedWindow("contrast image demo", CV_WINDOW_AUTOSIZE);
imshow("contrast image demo",dst);
waitKey(0);
return 0;
}
测试:
#include
#include
#include
using namespace cv;
int main(int argc, char** argv) {
Mat src, dst;
src = imread("D:/图片资源/1.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image",src);
/*
//获取图像宽度和高度,RGB图像有三个通道值
int cols = (src.cols-1) * src.channels();//宽度=图像列*通道数
int offsetx = src.channels();//图像通道
int rows = src.rows; //高度=图像高度
dst = Mat::zeros(src.size(), src.type());//初始化
for (int row = 1; row < (rows - 1); row++){
const uchar* previous = src.ptr(row - 1);//获前一行指针
const uchar* current = src.ptr(row);//获取当前行指针
const uchar* next = src.ptr(row + 1);//获取下一行指针
uchar* output = dst.ptr(row);
for (int col = offsetx; col < cols; col++) {
output[col] = saturate_cast(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
//加入函数saturate_cast();来限制范围
}
}
*/
//定义掩膜
double t = getTickCount();
Mat kernel = (Mat_(3, 3) << 0,-1,0,-1,5,-1,0,-1,0);
filter2D(src,dst,src.depth(),kernel);//src.depth可以直接改为-1
//解决方案花费的时间
double timeconsume = (getTickCount() - t) / getTickFrequency();
printf("tim consume %.2f",timeconsume);
namedWindow("contrast image demo", CV_WINDOW_AUTOSIZE);
imshow("contrast image demo",dst);
waitKey(0);
return 0;
}
RGB转化为BGR:
#include
#include
#include
using namespace cv;
using namespace std;
void my_channel_swap(Mat input_img)
{
//图像宽度和高度,RGB图像有三个通道值
Mat dst = Mat::zeros(input_img.size(), input_img.type());//初始化
int width = input_img.cols;
int height = input_img.rows;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
dst.at(j, i)[2] = input_img.at(j, i)[0];//R-B
dst.at(j, i)[1] = input_img.at(j, i)[1];//G-G
dst.at(j, i)[0] = input_img.at(j, i)[2];//B-R
}
}
imshow("通道交换输出图像", dst);
}
int main(int argc, char** argv) {
Mat src, dst;
src = imread("D:/图片资源/1.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image",src);
my_channel_swap(src);
/*提高图像对比度
//获取图像宽度和高度,RGB图像有三个通道值
int cols = (src.cols-1) * src.channels();//宽度=图像列*通道数
int offsetx = src.channels();//图像通道
int rows = src.rows; //高度=图像高度
dst = Mat::zeros(src.size(), src.type());//初始化
for (int row = 1; row < (rows - 1); row++){
const uchar* previous = src.ptr(row - 1);//获前一行指针
const uchar* current = src.ptr(row);//获取当前行指针
const uchar* next = src.ptr(row + 1);//获取下一行指针
uchar* output = dst.ptr(row);
for (int col = offsetx; col < cols; col++) {
output[col] = saturate_cast(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
//加入函数saturate_cast();来限制范围
}
}
*/
waitKey(0);
return 0;
}
灰度图像:(注意深度与通道数没设置对就会报错)
#include
#include
#include
using namespace cv;
void my_BGR2GRAY(Mat input_img)
{
int width = input_img.cols;
int height = input_img.rows;
Mat dst(406, 404, CV_8UC1);//8是位图深度,U是char类型,1是通道数(灰度!!!)
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
int b = input_img.at(j, i)[0];
int g = input_img.at(j, i)[1];
int r = input_img.at(j, i)[2];
dst.at(j, i) = saturate_cast(0.2126*r + 0.7152*g + 0.0722*b);
}
}
imshow("变换灰度之后", dst);
}
int main(int argc, char** aargv)
{
Mat src, dst;
src = imread("D:/图片资源/1.png");
if (!src.data)
{
printf("...");
return -1;
}
imshow("输入图像 ", src);
my_BGR2GRAY(src);
waitKey(0);
}