Crop a 1m * 1m image along a 60 ° line, with a cutting area of 0.1m * 0.1m rectangle, for a total of 100 images
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Brittney
el 14 de Jun. de 2024
Comentada: Brittney
el 15 de Jul. de 2024
% 导入BMP图像
img = imread('your_image.bmp');
% 原始图像尺寸
original_height = size(img, 1); % 图像高度
original_width = size(img, 2); % 图像宽度
% 生成底角线上的点
x0 = linspace(0, original_width, 100); % 在底部生成100个点
y0 = linspace(0, original_height, 100); % 在左侧生成100个点
% 60°线方程 y = tan(60°) * x
tan_60_deg = tan(deg2rad(60)); % 将角度转换为弧度
y_line = tan_60_deg * x0;
% 裁剪图片
crop_size = [0.1*original_height, 0.1*original_width]; % 裁剪尺寸
for i = 1:100
x_coord = round(x0(i)); % 取整数坐标
y_coord = round(y_line(i));
% 检查裁剪区域是否越界
if x_coord + crop_size(2) > original_width || y_coord + crop_size(1) > original_height
continue; % 越界则跳过
end
% 裁剪图片
cropped_img = img(y_coord:y_coord+crop_size(1)-1, x_coord:x_coord+crop_size(2)-1, :);
% 保存裁剪后的图片
imwrite(cropped_img, sprintf('cropped_image_%d.bmp', i));
end
Starting from the bottom of the photo, draw a line where a point on the line is a vertex of the rectangular box, but I cannot achieve it
Thank you for your answers.
My email is 1803905353@qq.com
0 comentarios
Respuesta aceptada
VINAYAK LUHA
el 11 de Jul. de 2024
Editada: VINAYAK LUHA
el 11 de Jul. de 2024
Hi Brittney,
My understanding is that you want to draw a line with a 60-degree slope starting from the bottom left corner of your image. Further, you want to crop 100 rectangles, each measuring 0.1 times the image dimensions along this line, ensuring that the bottom left corner of each square aligns with the 60-degree line.
Assuming the size of the image to be 500px x 500px, refer to the following code snippet for achieving the above objective -
img = zeros(500, 500, 3);
img(:,:,3) = 1;
% Display the image
imshow(img);
hold on; % Hold the image to draw rectangles on it
% Original image dimensions
original_height = size(img, 1); % Image height
original_width = size(img, 2); % Image width
% Generate points along the 60° line starting from the bottom-left corner
x0 = linspace(0, original_width, 100); % Generate 100 points along the x-axis
% Equation of the 60° line y = tan(60°) * x
tan_60_deg = tan(deg2rad(60)); % Convert angle to radians
y_line = tan_60_deg * x0;
plot(x0,original_height-y_line,color='g')
% Crop size (assuming 0.1 times the original dimensions)
crop_size = [0.1 * original_height, 0.1 * original_width]; % Square size
for i = 1:100
x_coord = round(x0(i));
y_coord = round(y_line(i));
% Adjust y_coord to start from the bottom-left corner
y_coord = original_height - y_coord;
% Check if the square region is out of bounds
if x_coord + crop_size(2) > original_width || y_coord - crop_size(1) < 1
continue; % Skip if out of bounds
end
% Draw the rectangle
rectangle('Position', [x_coord, y_coord - crop_size(1), crop_size(2), crop_size(1)], 'EdgeColor', 'r');
%Crop rectangle and save
cropped_img=imcrop(img, [x_coord, y_coord - crop_size(1), crop_size(2), crop_size(1)])
imwrite(cropped_img, sprintf('cropped_image_%d.bmp', i));
end
hold off; % Release the hold on the image
Hope this helps,
Thanks
Más respuestas (0)
Ver también
Categorías
Más información sobre Image Processing Toolbox en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!