技术专栏
一文学会opencv(一)——加载,修改,保存图像
加载,修改,保存图像
主要内容
imread()函数
2. cvtColor()函数
3. imwrite()函数
imread()函数
imread函数的功能是加载一张图像,并将其存储至一个Mat类型的对象中。第一个参数为图像文件名称,可以是相对路径,也可以是绝对路径。
第二个参数就是加载图像时,读入的类型。这里我们几个参数选择。如下图所示:
示例代码:
int main(){
Mat src; // 定义一个Mat类型对象
src = imread("./1.jpg", IMREAD_COLOR); // 读入一个图像
if (src.empty()){ // 当图像为空时,执行if语句判断
cout << "img empty" << endl; // 输出 img empty
}
imshow("src", src); // 显示图像
waitKey(0); // 设定等待时间为无限等待
return 0; // 返回值
}
cvtColor()函数
cvtColor()函数的功能是将一个图像从一个色彩空间转换到另一个色彩空间。需要三个参数,第一个参数为输入图像,第二个参数为输出图像。第三个参数为转换的色彩空间类型。
第三个参数种类如下所示:
cv::ColorConversionCodes {
cv::COLOR_BGR2BGRA = 0,
cv::COLOR_RGB2RGBA = COLOR_BGR2BGRA,
cv::COLOR_BGRA2BGR = 1,
cv::COLOR_RGBA2RGB = COLOR_BGRA2BGR,
cv::COLOR_BGR2RGBA = 2,
cv::COLOR_RGB2BGRA = COLOR_BGR2RGBA,
cv::COLOR_RGBA2BGR = 3,
cv::COLOR_BGRA2RGB = COLOR_RGBA2BGR,
cv::COLOR_BGR2RGB = 4,
cv::COLOR_RGB2BGR = COLOR_BGR2RGB,
cv::COLOR_BGRA2RGBA = 5,
cv::COLOR_RGBA2BGRA = COLOR_BGRA2RGBA,
cv::COLOR_BGR2GRAY = 6,
cv::COLOR_RGB2GRAY = 7,
cv::COLOR_GRAY2BGR = 8,
cv::COLOR_GRAY2RGB = COLOR_GRAY2BGR,
cv::COLOR_GRAY2BGRA = 9,
cv::COLOR_GRAY2RGBA = COLOR_GRAY2BGRA,
cv::COLOR_BGRA2GRAY = 10,
cv::COLOR_RGBA2GRAY = 11,
cv::COLOR_BGR2BGR565 = 12,
cv::COLOR_RGB2BGR565 = 13,
cv::COLOR_BGR5652BGR = 14,
cv::COLOR_BGR5652RGB = 15,
cv::COLOR_BGRA2BGR565 = 16,
cv::COLOR_RGBA2BGR565 = 17,
cv::COLOR_BGR5652BGRA = 18,
cv::COLOR_BGR5652RGBA = 19,
cv::COLOR_GRAY2BGR565 = 20,
cv::COLOR_BGR5652GRAY = 21,
cv::COLOR_BGR2BGR555 = 22,
cv::COLOR_RGB2BGR555 = 23,
cv::COLOR_BGR5552BGR = 24,
cv::COLOR_BGR5552RGB = 25,
cv::COLOR_BGRA2BGR555 = 26,
cv::COLOR_RGBA2BGR555 = 27,
cv::COLOR_BGR5552BGRA = 28,
cv::COLOR_BGR5552RGBA = 29,
cv::COLOR_GRAY2BGR555 = 30,
cv::COLOR_BGR5552GRAY = 31,
cv::COLOR_BGR2XYZ = 32,
cv::COLOR_RGB2XYZ = 33,
cv::COLOR_XYZ2BGR = 34,
cv::COLOR_XYZ2RGB = 35,
cv::COLOR_BGR2YCrCb = 36,
cv::COLOR_RGB2YCrCb = 37,
cv::COLOR_YCrCb2BGR = 38,
cv::COLOR_YCrCb2RGB = 39,
cv::COLOR_BGR2HSV = 40,
cv::COLOR_RGB2HSV = 41,
cv::COLOR_BGR2Lab = 44,
cv::COLOR_RGB2Lab = 45,
cv::COLOR_BGR2Luv = 50,
cv::COLOR_RGB2Luv = 51,
cv::COLOR_BGR2HLS = 52,
cv::COLOR_RGB2HLS = 53,
cv::COLOR_HSV2BGR = 54,
cv::COLOR_HSV2RGB = 55,
cv::COLOR_Lab2BGR = 56,
cv::COLOR_Lab2RGB = 57,
cv::COLOR_Luv2BGR = 58,
cv::COLOR_Luv2RGB = 59,
cv::COLOR_HLS2BGR = 60,
cv::COLOR_HLS2RGB = 61,
cv::COLOR_BGR2HSV_FULL = 66,
cv::COLOR_RGB2HSV_FULL = 67,
cv::COLOR_BGR2HLS_FULL = 68,
cv::COLOR_RGB2HLS_FULL = 69,
cv::COLOR_HSV2BGR_FULL = 70,
cv::COLOR_HSV2RGB_FULL = 71,
cv::COLOR_HLS2BGR_FULL = 72,
cv::COLOR_HLS2RGB_FULL = 73,
cv::COLOR_LBGR2Lab = 74,
cv::COLOR_LRGB2Lab = 75,
cv::COLOR_LBGR2Luv = 76,
cv::COLOR_LRGB2Luv = 77,
cv::COLOR_Lab2LBGR = 78,
cv::COLOR_Lab2LRGB = 79,
cv::COLOR_Luv2LBGR = 80,
cv::COLOR_Luv2LRGB = 81,
cv::COLOR_BGR2YUV = 82,
cv::COLOR_RGB2YUV = 83,
cv::COLOR_YUV2BGR = 84,
cv::COLOR_YUV2RGB = 85,
cv::COLOR_YUV2RGB_NV12 = 90,
cv::COLOR_YUV2BGR_NV12 = 91,
cv::COLOR_YUV2RGB_NV21 = 92,
cv::COLOR_YUV2BGR_NV21 = 93,
cv::COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21,
cv::COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21,
cv::COLOR_YUV2RGBA_NV12 = 94,
cv::COLOR_YUV2BGRA_NV12 = 95,
cv::COLOR_YUV2RGBA_NV21 = 96,
cv::COLOR_YUV2BGRA_NV21 = 97,
cv::COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21,
cv::COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21,
cv::COLOR_YUV2RGB_YV12 = 98,
cv::COLOR_YUV2BGR_YV12 = 99,
cv::COLOR_YUV2RGB_IYUV = 100,
cv::COLOR_YUV2BGR_IYUV = 101,
cv::COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV,
cv::COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV,
cv::COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12,
cv::COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12,
cv::COLOR_YUV2RGBA_YV12 = 102,
cv::COLOR_YUV2BGRA_YV12 = 103,
cv::COLOR_YUV2RGBA_IYUV = 104,
cv::COLOR_YUV2BGRA_IYUV = 105,
cv::COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV,
cv::COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV,
cv::COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12,
cv::COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12,
cv::COLOR_YUV2GRAY_420 = 106,
cv::COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420,
cv::COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420,
cv::COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2RGB_UYVY = 107,
cv::COLOR_YUV2BGR_UYVY = 108,
cv::COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY,
cv::COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY,
cv::COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY,
cv::COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY,
cv::COLOR_YUV2RGBA_UYVY = 111,
cv::COLOR_YUV2BGRA_UYVY = 112,
cv::COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY,
cv::COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY,
cv::COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY,
cv::COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY,
cv::COLOR_YUV2RGB_YUY2 = 115,
cv::COLOR_YUV2BGR_YUY2 = 116,
cv::COLOR_YUV2RGB_YVYU = 117,
cv::COLOR_YUV2BGR_YVYU = 118,
cv::COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2,
cv::COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2,
cv::COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2,
cv::COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2,
cv::COLOR_YUV2RGBA_YUY2 = 119,
cv::COLOR_YUV2BGRA_YUY2 = 120,
cv::COLOR_YUV2RGBA_YVYU = 121,
cv::COLOR_YUV2BGRA_YVYU = 122,
cv::COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2,
cv::COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2,
cv::COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2,
cv::COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2,
cv::COLOR_YUV2GRAY_UYVY = 123,
cv::COLOR_YUV2GRAY_YUY2 = 124,
cv::COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY,
cv::COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY,
cv::COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2,
cv::COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2,
cv::COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2,
cv::COLOR_RGBA2mRGBA = 125,
cv::COLOR_mRGBA2RGBA = 126,
cv::COLOR_RGB2YUV_I420 = 127,
cv::COLOR_BGR2YUV_I420 = 128,
cv::COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420,
cv::COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420,
cv::COLOR_RGBA2YUV_I420 = 129,
cv::COLOR_BGRA2YUV_I420 = 130,
cv::COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420,
cv::COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420,
cv::COLOR_RGB2YUV_YV12 = 131,
cv::COLOR_BGR2YUV_YV12 = 132,
cv::COLOR_RGBA2YUV_YV12 = 133,
cv::COLOR_BGRA2YUV_YV12 = 134,
cv::COLOR_BayerBG2BGR = 46,
cv::COLOR_BayerGB2BGR = 47,
cv::COLOR_BayerRG2BGR = 48,
cv::COLOR_BayerGR2BGR = 49,
cv::COLOR_BayerBG2RGB = COLOR_BayerRG2BGR,
cv::COLOR_BayerGB2RGB = COLOR_BayerGR2BGR,
cv::COLOR_BayerRG2RGB = COLOR_BayerBG2BGR,
cv::COLOR_BayerGR2RGB = COLOR_BayerGB2BGR,
cv::COLOR_BayerBG2GRAY = 86,
cv::COLOR_BayerGB2GRAY = 87,
cv::COLOR_BayerRG2GRAY = 88,
cv::COLOR_BayerGR2GRAY = 89,
cv::COLOR_BayerBG2BGR_VNG = 62,
cv::COLOR_BayerGB2BGR_VNG = 63,
cv::COLOR_BayerRG2BGR_VNG = 64,
cv::COLOR_BayerGR2BGR_VNG = 65,
cv::COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG,
cv::COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG,
cv::COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG,
cv::COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG,
cv::COLOR_BayerBG2BGR_EA = 135,
cv::COLOR_BayerGB2BGR_EA = 136,
cv::COLOR_BayerRG2BGR_EA = 137,
cv::COLOR_BayerGR2BGR_EA = 138,
cv::COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA,
cv::COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA,
cv::COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA,
cv::COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA,
cv::COLOR_BayerBG2BGRA = 139,
cv::COLOR_BayerGB2BGRA = 140,
cv::COLOR_BayerRG2BGRA = 141,
cv::COLOR_BayerGR2BGRA = 142,
cv::COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA,
cv::COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA,
cv::COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA,
cv::COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA,
cv::COLOR_COLORCVT_MAX = 143
}
示例代码:
int main(){
Mat src; // 定义一个Mat类型对象
Mat src_gray; // 定义一个Mat类型对象
src = imread("./1.jpg", IMREAD_COLOR); // 读入一个图像
if (src.empty()){ // 当图像为空时,执行if语句判断
cout << "img empty" << endl; // 输出 img empty
}
imshow("src", src); // 显示图像
cvtColor(src, src_gray, COLOR_RGB2GRAY); // 将src转换为灰度图像存储至src_gray
waitKey(0); // 设定等待时间为无限等待
return 0; // 返回值
}
imwrite()函数
imwrite()函数的功能是将图像保存到指定的路径下。参数有两个。第一个是存储路径,第二个是待存储的Mat对象。这个函数比较简单没什么好解释的。
示例代码:
int main(){
Mat src; // 定义一个Mat类型对象
Mat src_gray; // 定义一个Mat类型对象
src = imread("./1.jpg", IMREAD_COLOR); // 读入一个图像
if (src.empty()){ // 当图像为空时,执行if语句判断
cout << "img empty" << endl; // 输出 img empty
}
imshow("src", src); // 显示图像
cvtColor(src, src_gray, COLOR_RGB2GRAY); // 将src转换为灰度图像存储至src_gray
imwrite("./save.jpg", src_gray); // 将src_gray这个图像存储到相对路径下,并且命名为save.jpg
waitKey(0); // 设定等待时间为无限等待
return 0; // 返回值
}
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
点赞
收藏
评论
打赏
- 分享
- 举报
评论
1个
手气红包
-
四叶草~ 2023-04-18 14:34:30回复 举报good~~
相关专栏
-
浏览量:2424次2019-11-18 09:23:04
-
浏览量:462次2023-09-06 10:09:13
-
浏览量:5056次2021-02-26 17:38:02
-
浏览量:11458次2020-12-09 20:55:40
-
浏览量:690次2023-09-08 10:47:07
-
浏览量:3473次2020-02-28 16:25:43
-
浏览量:2952次2023-07-11 11:25:40
-
浏览量:1839次2019-11-11 10:43:40
-
浏览量:5687次2021-08-10 14:04:57
-
浏览量:2314次2020-08-03 16:35:15
-
浏览量:9175次2020-12-09 18:56:31
-
浏览量:2875次2020-12-07 09:55:15
-
浏览量:4616次2021-02-25 14:03:23
-
浏览量:790次2022-09-16 11:54:38
-
浏览量:2961次2020-11-10 14:02:39
-
浏览量:3470次2019-11-19 08:57:44
-
浏览量:9289次2020-12-04 13:56:35
-
浏览量:48901次2019-07-26 15:43:22
-
浏览量:12506次2021-06-15 10:26:13
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者
孔NERD
您的支持将鼓励我继续创作!
打赏金额:
¥1
¥5
¥10
¥50
¥100
支付方式:
微信支付
打赏成功!
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注