写一个Matlab抠图程序,手动选取8个抠图点,将这8个点形成的矩形区域抠出来并保存,图片的格式是.jpg
时间: 2024-05-25 21:15:41 浏览: 135
抠图程序如下:
clear all; close all; clc;
% 读入原始图像
img = imread('image.jpg');
% 显示原始图像
imshow(img);
title('请选择8个抠图点');
% 手动选择8个抠图点
[x,y] = ginput(8);
% 将8个点按逆时针顺序排序
pts = [x,y];
pts = sortrows(pts);
top_pts = pts(1:4,:);
bottom_pts = pts(5:8,:);
top_pts = sortrows(top_pts,[2,1]);
bottom_pts = sortrows(bottom_pts,[2,-1]);
pts = [top_pts;bottom_pts];
% 画出抠图区域
hold on;
plot([pts(1,1),pts(2,1),pts(3,1),pts(4,1),pts(1,1)],...
[pts(1,2),pts(2,2),pts(3,2),pts(4,2),pts(1,2)],'r-');
% 抠图并保存
rect = [min(pts(:,1)),min(pts(:,2)),max(pts(:,1))-min(pts(:,1)),max(pts(:,2))-min(pts(:,2))];
img_crop = imcrop(img,rect);
imwrite(img_crop,'image_crop.jpg','jpg');
% 显示抠图结果
figure;
imshow(img_crop);
title('抠图结果');
阅读全文