MATLAB图像处理(三):获取图像像素值、改变像素值及像素数

您所在的位置:网站首页 如何在ps上查看照片像素大小和像素 MATLAB图像处理(三):获取图像像素值、改变像素值及像素数

MATLAB图像处理(三):获取图像像素值、改变像素值及像素数

2024-07-16 03:48| 来源: 网络整理| 查看: 265

一、原理

图像由像素组成,imread 函数可读取图像每个像素的RGB值。 RGB图的每个像素的像素值一般由三个变量r、g、b组成(0-255)。 灰度图的每个像素的像素值一般只有一个值(0-255)。

二、获取像素值 2.1 impixel 函数

MATLAB关于此函数的帮助文档如下:

>> help impixel impixel - Pixel color values This MATLAB function lets you select pixels interactively from the image in the current axes. Select Pixels Interactively P = impixel P = impixel(I) P = impixel(X,cmap) Select Pixels by Specifying Coordinates P = impixel(I,xi,yi) P = impixel(X,cmap,xi,yi) P = impixel(xref,yref,I,xi,yi) P = impixel(xref,yref,X,cmap,xi,yi) Additionally Return Selected Pixel Coordinates [xi2,yi2,P] = impixel(___) 输入参数 I - Image numeric matrix | numeric array | logical matrix X - Indexed image matrix of integers cmap - Colormap c-by-3 numeric matrix xi - x-coordinate of pixels to sample numeric vector yi - y-coordinate of pixels to sample numeric vector xref - Image limits in world coordinates along x-dimension 2-element numeric vector yref - Image limits in world coordinates along y-dimension 2-element numeric vector 输出参数 P - Sampled pixel values p-by-3 matrix xi2 - x-coordinates of sampled pixels numeric vector yi2 - y-coordinates of sampled pixels numeric vector

可以根据坐标指定特定位置的像素值,举例:

pic = imread('keyan.jfif') ; r = 100 ; % 像素的行坐标,可为数组 c = 200 ; % 像素的列坐标,可为数组 imshow(pic) ; p = impixel(pic,r,c) ;

其中 r,c 分别为像素的行列坐标,用于指定像素的位置,可为同型数组。p存储所选像素的像素值(rgb值),每行一个像素点:

p = 79 169 219

也可使用鼠标选择要提取像素值的位置,举例:

pic = imread('keyan.jfif') ; imshow(pic) ; p = impixel(pic) ;

此时可以使用鼠标在图上点击像素点,回车后所点击像素点的像素值存于 p 矩阵中.

pic = imread('keyan.jfif') ; imshow(pic) ; h = impixelinfo ; % 显示鼠标光标位置的像素信息 % set(h,'position',[0 10 200 20]) % 设置 h 的位置

结果: 左下角显示鼠标光标位置的像素值

2.2 improfile 函数

此函数沿图像中一条直线或曲线路径抓取像素值:

I = imread('keyan.jfif') ; Ih = rgb2gray(I) ; imshow(Ih) ; improfile

选取曲线路径 路径像素点结果: 像素点 若直接对RGB图使用improfile函数,则出现r、g、b三条线,感兴趣者可以自行尝试。

2.3 imcontour 函数

此函数可显示灰度图的等值线轮廓,这个函数能够自动设置坐标轴对象,使得方向和长宽比能够与所显示的图像相匹配。

I = imread('keyan.jfif'); Ih = rgb2gray(I) ; subplot(121) imshow(I) subplot(122) imcontour(Ih,5) % 第二个参数为等值线数量

结果: 像素值等值线 注意此参数只能对二维(灰度图)操作,作用于RGB图时报维度错误。

2.4 获取 R G B 值

可单独获取RGB图像的r、g、b值,

I = imread('keyan.jfif') ; R = I(:,:,1) ; % r值 G = I(:,:,2) ; % g值 B = I(:,:,3) ; % b值 subplot(221) ; imshow(I) ; title('原图') ; subplot(222) imshow(R) ; title('R') ; subplot(223) imshow(G) ; title('G') ; subplot(224) imshow(B) ; title('B') ;

结果: RGB

三、像素修改 3.1 像素值修改

可根据像素点的位置编号修改其像素值。 如改变下面二值图的部分像素值,将某区域内的白像素点改为黑色

I = imread('keyan11.jpg') ; Ih = rgb2gray(I) ; subplot(121) imshow(I) ; % 某区域内的白像素点改为黑色 for i = 400:500 for j = 900:1000 if Ih(i,j) == 255 Ih(i,j) = 0 ; end end end subplot(122) imshow(Ih) ;

结果: 改变像素值

3.2 像素数修改

可用 imresize 函数改变像素的多少,如1000x1000改成500x500.

pic = imread('keyan.jfif'); % 读取图片 I = imresize(pic,[100,100]); % 修改图片像素尺寸中长和宽 figure(1) subplot(121) imshow(pic) title('原始图像'); subplot(122) imshow(I) title('修改后图像');

结果: 更改像素数 像素数得到改变:

>> size(I) ans = 100 100 3 >> size(pic) ans = 316 474 3

不好像从少像素向多像素转变没效果。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3