matlab图像处理趣味程序
时间: 2023-07-07 10:46:51 浏览: 98
以下是一个MATLAB图像处理的趣味程序,可以将一张彩色图片转换为黑白线稿:
1. 读取一张彩色图片,例如:
```matlab
img = imread('example.jpg');
```
2. 将彩色图片转换为灰度图片,例如:
```matlab
img_gray = rgb2gray(img);
```
3. 将灰度图片进行二值化处理,例如:
```matlab
img_bw = imbinarize(img_gray);
```
4. 对二值化后的图片进行膨胀操作,以增加线条的粗度,例如:
```matlab
se = strel('line',20,45); % 创建一个长度为20、角度为45度的线性结构元素
img_dilated = imdilate(img_bw, se);
```
5. 对膨胀后的图片进行边缘检测,提取出线条,例如:
```matlab
img_edge = edge(img_dilated, 'Canny');
```
6. 将边缘检测后的图片转换为RGB格式,并与原图叠加,即可得到黑白线稿,例如:
```matlab
img_edge_rgb = cat(3, img_edge, img_edge, img_edge);
img_sketch = img_edge_rgb .* double(img) + (1 - img_edge_rgb) .* 255;
imshow(uint8(img_sketch));
```
其中,cat()函数将边缘检测后的图片转换为RGB格式,double()函数将原图转换为浮点型,使得两张图片相乘时不会溢出,imshow()函数显示结果图片。
这个趣味程序可以让你通过简单的图像处理,将一张彩色图片转换为黑白线稿,可以用于制作DIY手绘风格的卡片、海报等创意设计。
阅读全文
相关推荐

















