[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
一、彩色图像转灰度
Opencv提供了一个方法,可以使彩色图像变为灰度图像。
函数名:cvtColor(src,dest,way);
src表示初始的mat对象;
dest表示转换后的mat对象;
way表示以何种方式转换。
举个例子:
int main() { //定义路径 string path = "Resources//test.png";
//Mat:opencv引入的矩阵数据类型,处理所有图像 Mat img = imread(path); //创建一个新的mat对象,用来存放转换后的灰度图像
Mat imgGray; //灰度转换函数 cvtColor(img, imgGray, COLOR_BGR2GRAY); //展示图片,并给图片取名
imshow("Image", img); //延迟,直到我们按下关闭按钮 imshow("ImageGray", imgGray); waitKey(0);
return 0; }
这里的方法就是COLOR_BGR2GRAY,
解释出来就是:RGB颜色to灰度。
运行截图:
二、高斯模糊
高斯模糊本质上是低通滤波器,输出图像的每个像素点是原图像上对应像素点与周围像素点的加权和。
函数:GaussianBlur(src,dest,size(m,n),sigma1,sigma2);
高斯模糊,第三,第四,第五参数为高斯模糊的度数。
定义内核大小为m*n,数字越大越模糊;
然后有sigma1和sigma2,这两个数也是模糊度数的参数,可定义为0。
//创建一个模糊对象 Mat imgBlur; //高斯模糊,第三,第四,第五参数为高斯模糊的度数, //定义内核大小为7*7,数字越大越模糊;
//然后有sigma1和sigma2,这两个数可定义为0 GaussianBlur(img, imgBlur, Size(7, 7),0,0);
imshow("ImageBlur", imgBlur); waitKey(0);
运行截图:
三、边缘检测
我们有很多边缘检测器,用于检测图像的边缘轮廓。
最常用的是坎尼边缘检测器。
Canny(src,dest,low,high);
检测前通常做模糊处理。
两个阈值:双阈值筛选
设置一个双阈值,即低阈值(low),高阈值(high)。
灰度变化大于high的,设置为强边缘像素,低于low的,剔除。
//坎尼边缘检测器 Mat imgCanny; //检测前通常做模糊处理 //两个阈值:双阈值筛选
//设置一个双阈值,即低阈值(low),高阈值(high)。 //灰度变化大于high的,设置为强边缘像素,低于low的,剔除。 Canny(imgBlur,
imgCanny, 50, 150); imshow("ImageCanny", imgCanny);
运行截图:
阈值为25,75: