W2-图像增强

线性变换

imag = imread('sherlock.jpg');
gray = rgb2gray(imag);
figure; 
subplot(2,2,1);
imshow(gray);
title('原图');
subplot(2,2,2);
imhist(gray);
title('原图灰度直方图');
a1=20;
b1=255;
a=50;
b=155;
image1=a1+(b1-a1)/(b-a)*(gray-a);
subplot(2,2,3);
imshow(image1);
title('线性变换后图像');
subplot(2,2,4);
imhist(image1);
title('线性变换后灰度直方图');

对数变换

imag = imread('office_1.jpg');
gray = rgb2gray(imag);
figure;subplot(2,2,1);
imshow(gray);
title('原图');
subplot(2,2,2);
imhist(gray);
title('原图灰度直方图');
c=1.2;
k=1.25;
image2=c*log(k+im2double(gray));
subplot(2,2,3);
imshow(image2);
title('对数变换后的图像');
subplot(2,2,4);
imhist(image2);
title('对数变换后的直方图');

%改进对数变换
figure;
subplot(2,2,1);
imshow(gray);
title('原图');
subplot(2,2,2);
imhist(gray);
title('原图灰度直方图');
v=500;
r=mat2gray(double(gray));
image2=log(1+v*r)/(log(v+1));
subplot(2,2,3);
imshow(image2);
title('改进对数变换后的图像');
subplot(2,2,4);
imhist(image2);
title('改进对数变换后的直方图');

Gamma变换

imag = imread('sherlock.jpg');
gray = rgb2gray(imag);
figure;
subplot(3,2,1);
imshow(gray);
title('原图');
subplot(3,2,2);
imhist(gray);
title('原图灰度直方图');
image3=imadjust(gray,[],[],0.6);
subplot(3,2,3);
imshow(image3);
title('Gamma=0.6变换后图像');
subplot(3,2,4);
imhist(image3);
title('Gamma=0.6变换后灰度直方图');
image4=imadjust(gray,[],[],1.8);
subplot(3,2,5);
imshow(image4);
title('Gamma=1.8变换后图像');

直方图均衡化

imag = imread('foosballraw.tiff');
gray=imag;
figure;
subplot(2,2,1);
imshow(gray);
title('原图');
subplot(2,2,2);
imhist(gray);
title('原图灰度直方图');
image1=histeq(gray);
subplot(2,2,3);
imshow(image1);
title('均衡化图像');
subplot(2,2,4);
imhist(image1);
title('均衡化后灰度直方图');

邻域平均法

imag = imread('yellowlily.jpg');
gray=rgb2gray(imag);
figure;
subplot(5,2,1);
imshow(gray);
title('原图');
subplot(5,2,2);
imhist(gray);
title('原图灰度直方图');
gauss_imag=imnoise(gray,'gaussian');
sp_imag=imnoise(gray,'salt & pepper');
subplot(5,2,3);
imshow(gauss_imag);
title('添加高斯噪声');
subplot(5,2,4);
imhist(gauss_imag);
title('添加高斯噪声后直方图');
subplot(5,2,5);
imshow(sp_imag);
title('添加椒盐噪声');
subplot(5,2,6);
imhist(sp_imag);
title('添加高斯噪声后直方图');
h_a=fspecial('average');
imag1=imfilter(gauss_imag,h_a);
subplot(5,2,7);
imshow(imag1);
title('邻域平滑图像');
subplot(5,2,8);
imhist(imag1);
title('邻域平滑直方图');
imag2=imfilter(sp_imag,h_a);
subplot(5,2,9);
imshow(imag2);
title('邻域平滑图像');
subplot(5,2,10);
imhist(imag2);
title('邻域平滑直方图');

中值滤波

imag = imread('yellowlily.jpg');
gray=rgb2gray(imag);
figure;
subplot(3,2,1);
imshow(gray);
title('原图');
subplot(3,2,2);
imhist(gray);
title('原图灰度直方图');
gauss_imag=imnoise(gray,'gaussian');
sp_imag=imnoise(gray,'salt & pepper');
subplot(3,2,3);
imshow(gauss_imag);
title('添加高斯噪声');
subplot(3,2,4);
imhist(gauss_imag);
title('添加高斯噪声后直方图');
subplot(3,2,5);
imshow(sp_imag);
title('添加椒盐噪声');
subplot(3,2,6);
imhist(sp_imag);
title('添加高斯噪声后直方图');
figure;
imag1=medfilt2(gauss_imag);
subplot(2,2,1);
imshow(imag1);
title('高斯噪声经中值滤波后图像');
subplot(2,2,2);
imhist(imag1);
title('高斯噪声经中值滤波后直方图');
imag2=medfilt2(sp_imag);
subplot(2,2,3);
imshow(imag2);
title('椒盐噪声经中值滤波后图像');
subplot(2,2,4);
imhist(imag2);
title('椒盐噪声经中值滤波后直方图');

拉普拉斯算子锐化

figure;
imag = imread('football.jpg');
gray=rgb2gray(imag);
subplot(2,2,1);
imshow(gray);
title('原图');
subplot(2,2,2);
imhist(gray);
title('原图灰度直方图');
im=im2double(gray);
w=fspecial('laplacian');
I1=imfilter(im,w,'replicate');
I2=im-I1;
subplot(2,2,3);
imshow(I2);
title('锐化后图像');
subplot(2,2,4);
imhist(I2);
title('锐化后直方图');

同态滤波

figure;
imag = imread('riceblurred.png');
gray=imag;
subplot(2,2,1);
imshow(gray);
title('原图');
subplot(2,2,2);
imhist(gray);
title('原图灰度直方图');
I=im2double(gray);
[M,N]=size(I);  
rL=0.5;  
rH=5; 
c=2;  
d0=10;  
I1=log(I+1);
FI=fft2(I1);
n1=floor(M/2);  
n2=floor(N/2);  
for i=1:M  
    for j=1:N  
        D(i,j)=((i-n1).^2+(j-n2).^2);  
        H(i,j)=(rH-rL).*(exp(c*(-D(i,j)./(d0^2))))+rL;
    end  
end  
I2=ifft2(H.*FI)
I3=real(exp(I2));  
subplot(2,2,3);
imshow(I3,[]);
title('同态滤波后图像');  
subplot(2,2,4);
I4 = I3-1;
imhist(I4);
title('同态滤波后直方图')

这个问题涉及到的内容比较多,需要分步骤来进行讲解。 ## 1. 环境搭建 首先需要安装OpenCV和Pillow库,可以使用pip进行安装: ``` pip install opencv-python pip install Pillow ``` ## 2. 摄像头实时采集图像 使用OpenCV库中的VideoCapture类可以实时采集摄像头的图像: ```python import cv2 cap = cv2.VideoCapture(0) # 0表示第一个摄像头 while True: ret, frame = cap.read() # ret表示是否成功读取到图像,frame表示读取到的图像 if ret: cv2.imshow('frame', frame) if cv2.waitKey(1) == ord('q'): # 按q键退出 break cap.release() cv2.destroyAllWindows() ``` ## 3. 图像处理 对于水稻种子的图像处理,可以先使用Pillow库将图像转换成灰度图,再使用OpenCV库中的二值化和形态学处理函数进行图像增强: ```python import cv2 from PIL import Image # 读取图像 img = cv2.imread('image.jpg') # 转换成灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) # 形态学处理 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) erode = cv2.erode(thresh, kernel, iterations=1) dilate = cv2.dilate(erode, kernel, iterations=1) # 显示处理后的图像 cv2.imshow('img', img) cv2.imshow('thresh', thresh) cv2.imshow('dilate', dilate) cv2.waitKey(0) cv2.destroyAllWindows() ``` ## 4. 标记水稻种子并计数 使用OpenCV库中的轮廓检测函数可以标记出水稻种子的位置,并使用len()函数计算出水稻种子的数量: ```python import cv2 from PIL import Image # 读取图像 img = cv2.imread('image.jpg') # 转换成灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) # 形态学处理 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) erode = cv2.erode(thresh, kernel, iterations=1) dilate = cv2.dilate(erode, kernel, iterations=1) # 轮廓检测 contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) count = len(contours) # 标记水稻种子 for i in range(count): x, y, w, h = cv2.boundingRect(contours[i]) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示处理后的图像 cv2.imshow('img', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` ## 5. 计算两个框之间的距离并绘制框 使用OpenCV库中的distance()函数可以计算出两个框之间的距离,并使用cv2.rectangle()函数绘制出相邻框之间的连线: ```python import cv2 from PIL import Image # 读取图像 img = cv2.imread('image.jpg') # 转换成灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) # 形态学处理 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) erode = cv2.erode(thresh, kernel, iterations=1) dilate = cv2.dilate(erode, kernel, iterations=1) # 轮廓检测 contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) count = len(contours) # 标记水稻种子 for i in range(count): x, y, w, h = cv2.boundingRect(contours[i]) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 绘制相邻框之间的连线 for i in range(count - 1): x1, y1, w1, h1 = cv2.boundingRect(contours[i]) x2, y2, w2, h2 = cv2.boundingRect(contours[i + 1]) cx1, cy1 = x1 + w1 // 2, y1 + h1 // 2 cx2, cy2 = x2 + w2 // 2, y2 + h2 // 2 cv2.rectangle(img, (x1, y1), (x1 + w1, y1 + h1), (0, 255, 0), 2) cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (0, 255, 0), 2) cv2.line(img, (cx1, cy1), (cx2, cy2), (0, 0, 255), 2) # 显示处理后的图像 cv2.imshow('img', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` ## 6. 信息汇总成表格 使用Pandas库可以将信息汇总成表格: ```python import cv2 import pandas as pd # 读取图像 img = cv2.imread('image.jpg') # 转换成灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) # 形态学处理 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) erode = cv2.erode(thresh, kernel, iterations=1) dilate = cv2.dilate(erode, kernel, iterations=1) # 轮廓检测 contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) count = len(contours) # 计算相邻框之间的距离 distances = [] for i in range(count - 1): x1, y1, w1, h1 = cv2.boundingRect(contours[i]) x2, y2, w2, h2 = cv2.boundingRect(contours[i + 1]) cx1, cy1 = x1 + w1 // 2, y1 + h1 // 2 cx2, cy2 = x2 + w2 // 2, y2 + h2 // 2 distance = ((cx2 - cx1) ** 2 + (cy2 - cy1) ** 2) ** 0.5 distances.append(distance) # 汇总信息成表格 df = pd.DataFrame({'种子数量': [count], '相邻框距离': distances}) # 显示表格 print(df) ``` ## 7. 完整代码 将以上步骤整合起来,得到完整代码如下: ```python import cv2 import pandas as pd # 读取图像 cap = cv2.VideoCapture(0) ret, img = cap.read() # 转换成灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) # 形态学处理 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) erode = cv2.erode(thresh, kernel, iterations=1) dilate = cv2.dilate(erode, kernel, iterations=1) # 轮廓检测 contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) count = len(contours) # 计算相邻框之间的距离 distances = [] for i in range(count - 1): x1, y1, w1, h1 = cv2.boundingRect(contours[i]) x2, y2, w2, h2 = cv2.boundingRect(contours[i + 1]) cx1, cy1 = x1 + w1 // 2, y1 + h1 // 2 cx2, cy2 = x2 + w2 // 2, y2 + h2 // 2 distance = ((cx2 - cx1) ** 2 + (cy2 - cy1) ** 2) ** 0.5 distances.append(distance) # 标记水稻种子 for i in range(count): x, y, w, h = cv2.boundingRect(contours[i]) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 绘制相邻框之间的连线 for i in range(count - 1): x1, y1, w1, h1 = cv2.boundingRect(contours[i]) x2, y2, w2, h2 = cv2.boundingRect(contours[i + 1]) cx1, cy1 = x1 + w1 // 2, y1 + h1 // 2 cx2, cy2 = x2 + w2 // 2, y2 + h2 // 2 cv2.rectangle(img, (x1, y1), (x1 + w1, y1 + h1), (0, 255, 0), 2) cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (0, 255, 0), 2) cv2.line(img, (cx1, cy1), (cx2, cy2), (0, 0, 255), 2) # 显示处理后的图像 cv2.imshow('img', img) cv2.waitKey(0) cv2.destroyAllWindows() # 汇总信息成表格 df = pd.DataFrame({'种子数量': [count], '相邻框距离': distances}) # 显示表格 print(df) # 保存处理后的图像 cv2.imwrite('result.jpg', img) cap.release() ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值