
figure(1),imshow(RGB),title('彩色图岁段')%显示彩色图片
I=rgb2gray(RGB)%彩色转化高并成灰度图
figure(2),imshow(I),title('灰度戚雀迹图')%显示灰度图
灰度是用单个通道表示图像亮尘,是图察桐像亮度的一种败键坦表示方法
RGB图像可通过如下公式转化为灰度图像
源码:
import cv2
import numpy as np
# Gray scale
def BGR2GRAY(img):
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
# Gray scale
out = 0.2126 * r + 0.7152 * g + 0.0722 * b
out = out.astype(np.uint8)
return out
# Read image
img = cv2.imread("../paojie.jpg").astype(np.float)
# Grayscale
out = BGR2GRAY(img)
# Show results
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
参考: https://www.cnblogs.com/wojianxin/p/12492234.html
%%bound2im为自定义函数,需放到MATLAB当前路径中一起运行I = imread('peppers.png')
red = 2*I(:,:,1)-I(:,:,2)-I(:,:,3)%超红色模型
t = graythresh(red)%OUTS自适应阈值
red_bw = im2bw(red,t)%二值化
%%%腐蚀
se = strel('disk',10)
red_imerode = imerode(red_bw,se)
%%%膨胀肆嫌
se = strel('disk',10)
red_dilate = imdilate(red_bw,se)
%%%第一种提取桥禅方法,不需要那个附加的自定义函数%%%
g = bwperim(red_dilate,4)
%%%%%第二种提取方法%%%%
%b = bwboundaries(red_dilate,'noholes',‘conn’,4)%轮廓提取
%%%找出敏雹尘最长边界
%d = cellfun('length',b)
%[maxd,k] = max(d)
%b = b{k}
%%%
%[m,n] = size(I)%获取原图像大小,用于确定边界图像大小
g = bound2im(b,m,n)%产生边界图像
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)