1

I am using the Matlab function imtransform to distort RGB images with conformal maps (examples https://www.flickr.com/photos/gbachelier/albums/72157677436918822).

In those images are empty regions that were filled with 'FillValues' (in the example images with 0 = black) possibly were the map is not defined. I would like to fill those regions directly with transparency for additional composite operations because if I would transfer all black pixels after imtransform to transparent (Make a pixel transparent in Matlab) this would possibly affect other regions. Any idea how to make those regions transparent without affecting the rest? Thank you very much!

Community
  • 1
  • 1
  • why would selecting that area and treating as transparent be an issue? seems like a straightforward enough way of doing it – Tasos Papastylianou Jan 24 '17 at 21:01
  • @ Tasos Papastylianou: if you mean "selecting by hand" that is out of the question. I need algorithmic approaches because this is part of evolutionary art processes with populations >= 10^3 individuals. – Günter Bachelier Jan 25 '17 at 11:28
  • No not manually, all the examples on that page involve a central connected component of all-black pixels, which presumably you want to make transparent. Select that central connected component (e.g. via `bwconn` / `bwlabel`) and do whatever you want with those pixels algorithmically. – Tasos Papastylianou Jan 25 '17 at 14:19
  • not to mention, all those examples the central components seem to be in the exact same position and size. Just apply a predefined mask and you're done. – Tasos Papastylianou Jan 25 '17 at 14:20

1 Answers1

0

You can use a white colored image ,all values are 255, and apply the same transformation to it and use the result as a mask for transparency.

Using the formula (you noted in comments) two images can be composed.

Example:

I1=double(imread('klXfF.jpg'));
I2=double(imread('9cEgo.jpg'));
G=double(imread('xHOtx.jpg'))/255;
I3 = uint8(bsxfun(@times, 1-G, I1 ) + bsxfun(@times, G, I2));
%In MATLAB R2016b this notation can be used
%  I3 = uint8((1-G) .* I1 + G .* I2);
imwrite(I3, "result.jpg");

I1I2

maskresult

rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • yes that would be the strategy that I would choose if it is a task in Perlmagick but I am not yet that much experienced in Matlab. Additional to the generation of a gray mask it would require the porting of the functionality of composite with CopyOpacity to Matlab. The direct filling in imtransform would be a much easier approach but if this is not possible I have to look into masks and composite in Matlab. – Günter Bachelier Jan 25 '17 at 11:23
  • I followed your proposal and made a gray mask (with a bit of blur postprocessing to soften the edges to avoid aliasing effects; optimized for a compose operation like copy-opacity): https://www.flickr.com/photos/gbachelier/32536660495/in/album-72157677436918822/ Could you give me a hint how to actually make a compositing operation in Matlab to mask out the black region with a soft transition over the gray values to the opaque white. Thanks! – Günter Bachelier Jan 27 '17 at 14:20
  • What do you mean by composition. Can you provide a minimal example. for example a 10 * 20 image as input and the expected output – rahnema1 Jan 27 '17 at 17:13
  • I mean Alpha-Compositing (RGBa) were the gray values [0 ... 255] of a mask were mapped to [0 1/255 ... 254/255 1] and used in the alpha layer. On the other hand one could skip this intermediate step and compose images I1 and I2 with a gray mask G directly as a weighted sum of pixel values: I3(i,j) = (1 - G(i,j)/255) * I1(i,j) + G(i,j) * I2(i,j) in analogy to https://de.mathworks.com/help/vision/ref/compositing.html (have not vision toolbox). To avoid the for-loops it is perhaps possible to formulate this directly as I3 = (1-G/255) * I1 + G/255 * I2. – Günter Bachelier Jan 28 '17 at 09:20
  • @ rahnema1: the vectorized formula I3 = uint8((1-G) .* I1 + G .* I2); was exactly what I was looking for. Thank you very much!! – Günter Bachelier Jan 29 '17 at 18:35
  • I included the solution in my code and it works but from the perspective of my ImageMagick workflow I first find it counter intuitive: I see the abstract image as foreground (with black as transparency) and the watermelon as background so I first thought foreground over background would be I = uint8((1-G) .* foreground + G .* background) which is wrong but I = uint8(G .* foreground + (1-G) .* background) works, so I must rethink: If black in foreground is transparency and that's were G = 0 the background should be seen there so the foreground must be 0 which is done by (G .* foreground). – Günter Bachelier Jan 31 '17 at 19:14
  • I will be glad if I see result of your work in the album – rahnema1 Jan 31 '17 at 19:25
  • I made a test set with a first simple compose strategy with Background Bg = imresize(C, [cm_out_h cm_out_w]) and apply the compose operation T = uint8(CM_mask_blur_alpha .* T + (1 - CM_mask_blur_alpha) .* Bg) see https://www.flickr.com/photos/gbachelier/albums/72157679863538505. Inspecting the results with a image editor shows a disappointing quality because the edges of the former black regions showing massive aliasing. – Günter Bachelier Feb 02 '17 at 10:41
  • Blurring the mask is a strategy that would help in ImageMagick's CopyOpacity but is unfruitful here so I'm suspecting the composing uses the mask like a binary insted of a gray mask or with the mask itself is something wrong. I generate it by the sequence: whiteImage = 255 * ones(cm_out_h, cm_out_w, 'uint8'); CM_mask = imtransform(...), CM_mask_blur = imgaussfilt(...), CM_mask_blur_alpha = CM_mask_blur/255. I tried first something with G=double(...) like your example but I got an error from Matlab. – Günter Bachelier Feb 02 '17 at 10:42
  • If you want to use a binary mask you can do this: `result = abstract_image; result(mask==0) = watermelon(mask==0);` – rahnema1 Feb 02 '17 at 13:49
  • it is official: the gray mask is used as a binary mask (deduced from an error message: Array dimensions must match for binary array op.). Binary masks cause unacceptable aliasing effects so all this was worthless. – Günter Bachelier Feb 03 '17 at 20:37
  • I made a post processing script with PerlMagick that uses CopyOpacity to show the continuous transition of fore- and background when using a gray mask instead of a binary mask: https://www.flickr.com/photos/gbachelier/sets/72157679935822485 To replicate this functionality in Matlab was the goal. – Günter Bachelier Feb 04 '17 at 10:07