当前位置:首页 > 代码 > 正文

matlab裁剪代码(matlab裁剪图片代码)

admin 发布:2022-12-19 10:01 142


今天给各位分享matlab裁剪代码的知识,其中也会对matlab裁剪图片代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

MATLAB中如何批量剪裁彩色图片

MATLAB中如何批量剪裁彩色图片

答:

使用matlab批量的在图片中裁剪指定像素大小的图片,附:matlab代码

for

k=1:1

p=k;

q=int2str(p);

path='G:\plant数据\P4\1h\';

i=imread(strcat(path,num2str(k),'.tif'));

[I2,rect]

=

imcrop(i,[225,225,575,575]);

%若有一个图像为i,在matlab中进行如下操作,imcrop的参数为[2,2,2,2]

%代表左上角坐标为[2,2],裁剪的长度2,宽度为2。

figure(1);

imshow(i);

hold

on

x1=rect(1);

x2=rect(2);

w=rect(3);

h=rect(4);

rectangle('Position',[x1,x2,w,h],

'EdgeColor','y');

x3=x1+w;

x4=x2+h;

hold

off

figure(2);

imshow(I2);

imwrite(I2,strcat('G:\plant数据\',num2str(k),'.tif'));

end

matlab 批量读入、裁剪并输出图像

close all;

clc;

clear;

file_path = 'D:\input\';

img_path_list = dir(strcat(file_path,'*.jpg'));

img_num = length(img_path_list);

if img_num 0

for j = 1:img_num

image_name = img_path_list(j).name;

image = imread(strcat(file_path,image_name));

if image(400,360)==0

for ii=1:600

for jj=1:599

imag(ii,jj,:)=image(ii+419,jj+60,:);

end

end

imwrite(imag,['D:\output1' , '\', 'mm' num2str(j) '.jpg']);

elseif image(350,360)==0

for ii=1:636

for jj=1:716

imag(ii,jj,:)=image(ii+360,jj+2,:);

end

end

imwrite(imag,['D:\output2' , '\', 'mm' num2str(j) '.jpg']);

else

imag=image;

imwrite(imag,['D:\output3' , '\', 'mm' num2str(j) '.jpg']);

end

end

end

另外不知道楼主判断语句image(350,360)==0和image(400,360)==0试想判断像素点是否是黑色只想判断r分量是否是0,如果是前者需要改代码如下:

if image(400,360,1)==0image(400,360,2)==0image(400,360,3)==0

elseif同理。。。

用matlab如何讲图片的其中一部分进行裁剪,然后对裁剪的部分进行上下倒转之后再放回原图啊??

使用 imcrop 时,返回两个值即可简单实现:

[jm, rect] = imcrop(im)

上面的变量意义:im 是需要处理的原始图片,jm 是图片裁剪部分,rect 是坐标

其中,rect 是一个包含 4 个元素的数组,前两个元素是裁剪部分左上角的坐标 x 和 y,后两个元素是裁剪部分的大小,格式为 [xpos, ypos, xlen, ylen]。注意,x 方向是宽,y方向是高,而 Matlab 是列优先索引,因此实际索引时需要将 y 方向作为第一维;

你的代码可以这样写:

%read image

img = imread('baby.jpg');

figure;

subplot(1,2,1)

%show image

imshow(img);

title('ORIGINAL IMAGE');

%crop image

[~, rect] = imcrop(img);

rect = num2cell(round(rect));

[xpos, ypos, xlen, ylen] = deal(rect{:});

%Upside down

imud = im;

imud(ypos:ypos+ylen, xpos:xpos+xlen, :) = flipud(imud(ypos:ypos+ylen, xpos:xpos+xlen, :));

subplot(1,2,2);

imshow(imud);

代码示例

相比原代码,变动主要在 crop image 部分

运行结果

另附一个具有参数检查功能的函数如下,可实现根据坐标进行部分翻转,首个参数为图片变量,第二个参数(可选)决定坐标的类型,'a' 是绝对坐标,表示输入数组下标,'r' 是相对坐标,表示坐标在全图的比例(范围是0-1),具体代码如下:

function ret = partflipud(varargin)

%PARTFLIPUD Flip part of an array up to down.

%% Check Number of Arguments

narginchk(1, 6);

nargoutchk(0, 1);

%% Check Validation of Arguments im

im = varargin{1};

validateattributes(im, {'numeric'}, {'3d', 'nonnan'});

args = nargin - 1;

if args == 0

  ret = flipud(im);

  return

end

%% Processing Other Arguments

if args 0

  abspos = false; %'absolute'

  if ischar(varargin{2}) || isstring(varargin{2})

      postype = varargin{2};

      if any(strcmpi(postype, {'absolute', 'a', 'abs'}))

          abspos = true;

      elseif any(strcmpi(postype, {'relative', 'r', 'rel'}))

          abspos = false;

      else

          error(['Unrecognized argument postype: ', postype]);

      end

      args = args - 1;

      if args == 0

          error('Missing argument position.');

      end

  end

  if args == 1

      pos = varargin{3};

      args = numel(pos);

  else

      pos = varargin(end-args+1:end);

  end

  if args ~= 4

      error(['Expected 4 position arguments, got ', num2str(args)]);

  end

  [x1, y1, x2, y2] = pos{:};

  if x1 x2

      error('coordinate x1 greater than x2');

  elseif y1 y2

      error('coordinate y1 greater than y2');

  end

  if ~abspos

      [xlen, ylen] = size(im);

      x1 = floor(x1 * xlen);

      y1 = floor(y1 * ylen);

      x2 = ceil (x2 * xlen);

      y2 = ceil (y2 * ylen);

  end

end

%% Flip Specified Part of Array

ret = im;

ret(y1:y2, x1:x2, :) = flipud(ret(y1:y2, x1:x2, :));

end % function partflipud

参数检查部分

函数第 4-14 行检查输入和输出参数的合法性,支持输入灰度图像和 RGB/RGBA 格式图像;

坐标类型判断

函数第 15-31 行判断输入坐标的类型,绝对坐标可以用 'absolute', 'a', 'abs' 表示,绝对坐标可以用 'relative', 'r', 'rel',默认使用相对坐标;

坐标参数预处理和图片裁剪翻转处理

函数余下部分进行坐标处理和图像裁剪翻转;

如何用matlab对图像进行剪切

matlab图像的剪切

函数imcrop实现对图像的剪切操作。格式如下:

B=imcrop(A);

B=imcrop(X,map)

B=imcrop(RGB)

以上实现交互式的对灰度图像,索引图像和真彩色图像的剪切操作。

B=imcrop(I,rect)

B=imcrop(A,map,rect)

B=imcrop(RGB,rect)

分别指定举行区域rect剪切图像,rect是一个4元向量[xmin,ymin,width,height]。[B,rect]=imcrop(…)

[x,y,B,rect]=imcrop(..)返回剪切框参数。

MATLAB简单

1)划分子区域

方法一:

图像I是个矩阵,划分为子区域就是按一定规律取矩阵的对应行列。

例如I   100*100矩阵,10*10个子区域为:

则其

第一块为:(1:10,1:10)    子区域编号为(1,1)

第二块为:(11:20,1:10)  子区域编号为(2,1)

以此类推:

可以通过循环来实现:

for i=1:10;

   for j=1:10;

   eval(['I' num2str(i) num2str(j) '=I(1+10*(i-1):10+10*(i-1),1+10*(j-1):10+10*(j-1))']);

   end

end

每个子区域为I11 I12 I13...I1010

示例:

方法二:通过裁剪命令,裁剪出对应的区域:

I2=imcrop(I,RECT)

RECT是对应区域的边界

比如:

I11=imcrop(I,[1,1,10,10]);即子区域的左上角点坐标和右下角点坐标。

示例代码如下:

for i=1:10;

   for j=1:10;

   eval(['I' num2str(i) num2str(j) '=imcrop(I,[1+10*(i-1),1+10*(i-1),10+10*(j-1),10+10*(j-1)])']);

   end

end

2)直方图的求取和画法:

[x,n]=hist(I(:),1:255);

% x返回横坐标数组-----频数统计的小区间的中点

% n返回纵坐标数组-----各小区间内的频数

plot(n,x)

此外也可以直接用:

p=imhist(I);

plot(p);

概率密度:plot(p/N);

%N为区域总点数,如10*10。

3)将划分区域和灰度直方图一起使用:

for i=1:10;

    for j=1:10;

    p(10*(i-1)+j,:)=imhist(I(1+10*(i-1):10+10*(i-1),1+10*(j-1):10+10*(j-1)));

    end

end

则每个子区域的直方图统计数据在p矩阵的每一行中,共计100行。

MATLAB 图片裁剪处理

原图像为F

[f,rect] = imcrop(F)

f 为裁剪所得图片,rect 为f 的起始点以及长宽值。

matlab裁剪代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于matlab裁剪图片代码、matlab裁剪代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/6362.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载