MATLAB图像处理(三):获取图像像素值、改变像素值及像素数 |
您所在的位置:网站首页 › 如何在ps上查看照片像素大小和像素 › MATLAB图像处理(三):获取图像像素值、改变像素值及像素数 |
一、原理
图像由像素组成,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 的位置结果: 此函数沿图像中一条直线或曲线路径抓取像素值: I = imread('keyan.jfif') ; Ih = rgb2gray(I) ; imshow(Ih) ; improfile
此函数可显示灰度图的等值线轮廓,这个函数能够自动设置坐标轴对象,使得方向和长宽比能够与所显示的图像相匹配。 I = imread('keyan.jfif'); Ih = rgb2gray(I) ; subplot(121) imshow(I) subplot(122) imcontour(Ih,5) % 第二个参数为等值线数量结果: 可单独获取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') ;结果: 可根据像素点的位置编号修改其像素值。 如改变下面二值图的部分像素值,将某区域内的白像素点改为黑色 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) ;结果: 可用 imresize 函数改变像素的多少,如1000x1000改成500x500. pic = imread('keyan.jfif'); % 读取图片 I = imresize(pic,[100,100]); % 修改图片像素尺寸中长和宽 figure(1) subplot(121) imshow(pic) title('原始图像'); subplot(122) imshow(I) title('修改后图像');结果: 不好像从少像素向多像素转变没效果。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |