粘贴裁剪后的图像而不改变颜色
Paste Cropped image without changing color
我正在尝试通过合并裁剪图像来向图像添加更多元素。我的代码工作正常,但我在合并时裁剪的图像不保留颜色。
B = A(y1:y2,x1:x2);
subplot(2, 2, 3);
imshow(B);
axis on;
[rows2, columns2] = size(B);
promptMessage = sprintf('Click on the upper left point where you want to paste it,\nor click Cancel to quit.');
titleBarCaption = 'Continue?';
[x, y] = ginput(1);
r1 = int32(y);
c1 = int32(x);
r2 = r1 + rows2 - 1;
r2 = min([r2 rows]);
c2 = c1 + columns2 - 1;
c2 = min([c2, columns]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
A(r1:r2, c1:c2) = B(1:(r2-r1+1), 1:(c2-c1+1));
subplot(2, 2, 4);
imshow(A);
最终图像已裁剪图像,但裁剪图像的颜色 changes.Please 让我知道如何保留裁剪图像的颜色。
谢谢。
它不保留颜色,因为您只是提取图像的第一个通道。我假设您的图像是 RGB 图像,因此您还需要在相同的空间坐标处提取所有通道。因此,你也需要切入三维:
B = A(y1:y2,x1:x2,:); %// Change
subplot(2, 2, 3);
imshow(B); % Imange B is a cropped image with same color
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
A(r1:r2, c1:c2,:) = B(1:(r2-r1+1), 1:(c2-c1+1),:); %// Change
subplot(2, 2, 4);
imshow(A);
我正在尝试通过合并裁剪图像来向图像添加更多元素。我的代码工作正常,但我在合并时裁剪的图像不保留颜色。
B = A(y1:y2,x1:x2);
subplot(2, 2, 3);
imshow(B);
axis on;
[rows2, columns2] = size(B);
promptMessage = sprintf('Click on the upper left point where you want to paste it,\nor click Cancel to quit.');
titleBarCaption = 'Continue?';
[x, y] = ginput(1);
r1 = int32(y);
c1 = int32(x);
r2 = r1 + rows2 - 1;
r2 = min([r2 rows]);
c2 = c1 + columns2 - 1;
c2 = min([c2, columns]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
A(r1:r2, c1:c2) = B(1:(r2-r1+1), 1:(c2-c1+1));
subplot(2, 2, 4);
imshow(A);
最终图像已裁剪图像,但裁剪图像的颜色 changes.Please 让我知道如何保留裁剪图像的颜色。
谢谢。
它不保留颜色,因为您只是提取图像的第一个通道。我假设您的图像是 RGB 图像,因此您还需要在相同的空间坐标处提取所有通道。因此,你也需要切入三维:
B = A(y1:y2,x1:x2,:); %// Change
subplot(2, 2, 3);
imshow(B); % Imange B is a cropped image with same color
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
A(r1:r2, c1:c2,:) = B(1:(r2-r1+1), 1:(c2-c1+1),:); %// Change
subplot(2, 2, 4);
imshow(A);