使用MATLAB读取图片计算选定区域的亮度
时间: 2024-05-12 21:20:56 浏览: 6
以下是使用MATLAB读取图片计算选定区域的亮度的示例代码:
1. 读取图片
```
img = imread('example.jpg');
```
2. 显示图片
```
imshow(img);
```
3. 选定区域
使用`imrect`函数选择矩形区域:
```
rect = imrect;
position = wait(rect);
x = round(position(1));
y = round(position(2));
width = round(position(3));
height = round(position(4));
```
4. 计算亮度
将图像转换为灰度图像,然后计算选定区域的平均亮度值:
```
gray_img = rgb2gray(img);
selected_region = gray_img(y:y+height-1, x:x+width-1);
mean_brightness = mean(selected_region(:));
```
完整代码示例:
```
img = imread('example.jpg');
imshow(img);
rect = imrect;
position = wait(rect);
x = round(position(1));
y = round(position(2));
width = round(position(3));
height = round(position(4));
gray_img = rgb2gray(img);
selected_region = gray_img(y:y+height-1, x:x+width-1);
mean_brightness = mean(selected_region(:));
disp(['Mean brightness of selected region: ' num2str(mean_brightness)]);
```