3
$\begingroup$

I have an image and I would like to retrieve all color values of pixels which are located along stribes:

enter image description here

So what I do, is the following:

  1. I import the image from the web
  2. I get the image dimensions (w,h)
  3. I choose the number of stribes (rows) -> here:4
  4. I chose the number of pixels to examine for each row
  5. Using a nested table I can get a nested list of the color values

Here is the code:

im = Import[
   "https://images.pexels.com/photos/67636/rose-blue-flower-rose-\
blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"];
imD = ImageDimensions[im];
(*Width and Height*)
w = imD[[1]];
h = imD[[2]];

(*Input*)
numberOfRows = 4;
numberOfPixelsPerRow = 50;

(*Calculation*)
incrementX = Round[w/(numberOfPixelsPerRow-1)];
incrementY = Round[h/(numberOfRows-1)];

horizontal =
  Table[
   Table[
    PixelValue[im, {x, y}], {x, 0, imD[[1]], incrementX}],
   {y, 0, imD[[2]], incrementY}];

Now, I would like to visualize the color values in such a way:

A half transparent image of the original image, and only the retrieved pixels are not half transparent.

How can I do that ?

$\endgroup$
1
$\begingroup$

We start by importing the image and making it smaller to better fit the width of this website:

im = Import["https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"];
im = ImageResize[im, 500]

Mathematica graphics

The next step is to extract the pixels. This can be done more efficiently than the way you're doing it. I'm showcasing this other way:

{w, h} = ImageDimensions[im];
numberOfRows = 50;
numberOfPixelsPerRow = 300;
rowsIncrement = Round[h/numberOfRows];
colsIncrement = Round[w/numberOfPixelsPerRow];
data = ImageData[SetAlphaChannel[im]];
selectedPixels = data[[;; ;; rowsIncrement, ;; ;; colsIncrement, ;;]];
Image[selectedPixels, Options@im]

Mathematica graphics

The image above is made out of all the selected pixels. The Options@ part is needed to set the color profile to be the same as in the original image, the colors won't render the same otherwise. I just learned this form MichaelE2.

Note also that I used SetAlphaChannel to create a fully opaque alpha channel (opacity is 1 everywhere). Next, we create a copy of the image with an alpha channel of 0.5. We then replace selected pixels in this image with the ones that we previously extracted from the opaque version of the image:

newImage = SetAlphaChannel[im, 0.5];
newData = ImageData[newImage];
newData[[;; ;; rowsIncrement, ;; ;; colsIncrement, ;;]] = selectedPixels;
Image[newData, Options@im]

Mathematica graphics

If we look closely at this image we can clearly see that some pixels are different from their surrounding pixels, creating dotted lines across the image.

$\endgroup$
2
$\begingroup$

Is that what you need?

n = 4;  (*number of stripes*)
d = 20; (*height of stripes in pixels *)

dim = ImageDimensions[img][[2]];

HighlightImage[img,
  { EdgeForm @ None, Table[
      Rectangle[
        Scaled[{0, i/(n + 1) - d/2/dim}]
      , Scaled[{1, i/(n + 1) + d/2/dim}]
      ]
    , {i, n}
    ]
  }
, {"Lighten", .5}
]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks a lot ! This already looks good. However, I really want to only see the pixels of which I extracted the color data. Can this be done somehow with the extracted values ? $\endgroup$ – james Aug 14 '18 at 12:20
  • $\begingroup$ @james you mean not only stripes should be highlighted but also all places that have values which exist in those tripes? $\endgroup$ – Kuba Aug 14 '18 at 13:02
  • $\begingroup$ Something like horizontal = Table[ Table[ {x, y}, {x, 0, imD[[1]], incrementX}], {y, 0, imD[[2]], incrementY}]; HighlightImage[im, horizontal, {"Lighten", 0.5}]] $\endgroup$ – C. E. Aug 14 '18 at 15:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.