简体   繁体   English

在MATLAB中的图像中绘制黑色矩形

[英]Drawing a black rectangle in an image in MATLAB

I'm facing a problem. 我遇到了一个问题。 I'm writing a script and wanted to apply this effect on a white image. 我正在编写脚本,并希望将此效果应用于白色图像。

在此处输入图片说明

But now my image appears like this. 但是现在我的形象看起来像这样。

在此处输入图片说明

I'm stuck on this i write two for loops one for rows and second for columns which draw black lines according to image size 800x450 but how i draw black boxes. 我坚持这一点,我写了两个for循环,一个用于行,第二个用于列,它们根据图像尺寸800x450绘制黑线,但是我如何绘制黑框。

Script: 脚本:

        clear all;
        close all;
        clc;
        image = rgb2gray(imread('4.jpg'));
        b = cast(image,'double');
        out = 60*((b)./(max(max(b))));

        out = out + 195;

        [r,c,d] = size(out);

        x = floor(r/80);

        y = floor(c/160);

        %x rows
        for i = 1 : 88 : 450
            %  replaceable line
            out(i:i+x,:,:) = 0;

        end



        %y columns
        for i = 1 : 88 : 800
            %  replaceable line
            out(:,i:i+y,:) = 0;
        end

        final = cast(out,'uint8');

        imshow(final);

The trick for drawing a checkerboard, is to find cases where the x and y indices (when added together) are either odd or even. 绘制棋盘的诀窍是找出x和y索引(加在一起时)为奇数或偶数的情况。

sz = size(img);
[row, col] = ndgrid(1:sz(1), 1:sz(2));
checkers = logical(mod(row + col, 2));

Or more simply 或更简单

checkers = bsxfun(@(x,y)mod(x + y, 2), 1:size(img, 1), (1:size(img, 2)).').';

This will return a matrix with 0 (black) and 1 (white) checkers. 这将返回带有0(黑色)和1(白色)检查器的矩阵。

在此处输入图片说明

If you want to change the size of the checkers you could do something like this. 如果要更改检查器的大小,可以执行以下操作。

sz = size(img);
checkersize = 10;
[row, col] = ndgrid(1:sz(1), 1:sz(2));
checkers = logical(mod(ceil(row / checkersize) + ceil(col/checkersize)),2);

Or simply 或者简单地

checkersize = 10;
checkers = bsxfun(@(x,y)mod(ceil(x/checkersize) + ceil(y/checkersize), 2), 1:size(img, 1), (1:size(img, 2)).').';

在此处输入图片说明

If you actually want to apply this effect to an image, you can use the checkers matrices above to index into your image and set values similar to this answer 如果您确实想将此效果应用于图像,则可以使用上面的checkers矩阵索引到图像中并设置类似于此答案的

replacementColor = [0 0 0];                         % RGB
reshaped_image = reshape(img, [], 3);               % Flatten first two dims
C = repmat(replacementColor, [sum(checkers(:)), 1]);% Make color correct size
reshaped_image(checkers, :) = C;                    % Replace color
newImage = reshape(reshaped_image, size(img));      % Shape it back into original

You can use this part of code: 您可以使用以下部分代码:

image = ones(450,800,3);%create white image
[row_len,column_len,rgb] = size(image);
side_vert = floor(row_len/5);
side_hor  = floor(column_len/9);
color = 1;
for(i = 1:side_vert:row_len)
    for(j = 1:side_hor:column_len - side_hor)
        if(color == 0)
            image(i:side_vert - 1 + i,j:j + side_hor - 1,:) = 0;
            color = 1;
        else
            color = 0;
        end;
    end;
end;
imshow(image);

You can also use the checkerboard function: 您还可以使用棋盘功能:

k = 30;   %// size of each square in the checkerboard
img = checkerboard(k) < 0.5;   

For some reason the function makes half of the checkerboard gray, so you have to use < 0.5 (or > 0.5 , depending on the ordering you want) to give you just the black and white image. 由于某种原因,该功能会使棋盘格的一半变成灰色,因此您必须使用< 0.5 (或> 0.5 ,具体取决于所需的顺序)才能获得黑白图像。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM