In matlab, how to draw a grid over an image

前端 未结 3 1425
时光取名叫无心
时光取名叫无心 2020-12-10 16:12

How to draw a grid over an image. It should become part of that image itself. It should be able to show some rows and columns over the image itself. The lines for rows and c

3条回答
  •  醉话见心
    2020-12-10 17:00

    There are a number of related questions on SO that discuss ways to modify an image. Here are the two general approaches:

    1. Modify the image data directly: I discuss this in my answer to this other SO question. Since image data can be 2-D or 3-D, you can use multidimensional indexing to modify the raw image data, creating lines along given rows and columns. Here's an example that changes every 10 rows and columns in the image to black:

    img = imread('peppers.png');  %# Load a sample 3-D RGB image
    img(10:10:end,:,:) = 0;       %# Change every tenth row to black
    img(:,10:10:end,:) = 0;       %# Change every tenth column to black
    imshow(img);                  %# Display the image
    

    alt text

    And now the image data in the variable img has black lines on it, and you can write it to a file or do whatever other processing you want to it.

    2. Plot the image and the lines, then turn the axes/figure into a new image: The link to Steve Eddins' blog in zellus' answer shows an example of how you can plot an image and add lines to it. However, if you want to save or perform processing on the displayed image, you will have to save the displayed image as an image matrix. How you can do this has been discussed in these other SO questions:

    • How can I save an altered image in MATLAB?
    • Turn Matlab plot into image

提交回复
热议问题