2
$\begingroup$

I have an image img, and a region iTrim as below:-

img = ExampleData[{"TestImage", "House"}]
iTrim = ImageTrim[img, {{30, 150}, {230, 210}}]

enter image description here

My desired output is, an image of img, but all the region of iTrim would be totally white in color.

How can I get such an image?

Many thanks!

$\endgroup$
  • $\begingroup$ Did any of the 5 answers satisfied your need? There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one and all that, but one weeks is enough wait. Participation is essential for the site, please do your part. $\endgroup$ – rhermans Jul 17 '18 at 18:52
3
$\begingroup$

Simple solution:

{w, h} = ImageDimensions[img];
{left, right, top, bottom} = {30, 230, h - 210, h - 150};

data = ImageData[img];
data[[top ;; bottom, left ;; right]] = {1, 1, 1};
Image[data]

Mathematica graphics

$\endgroup$
8
$\begingroup$

This is very short:

ImageSubtract[#, ImageAlign[#, iTrim]] &@img

Mathematica graphics

$\endgroup$
4
$\begingroup$

You could use Graphics to create a mask image:

mask = Rasterize[
  Style[Graphics[{White, Rectangle[{30, 150}, {230, 210}]}, 
    PlotRange -> Transpose[{{0, 0}, ImageDimensions[img]}], 
    ImageSize -> ImageDimensions[img], Background -> Black], 
   Antialiasing -> False]]

enter image description here

The important thing is that the Graphics has the same resolution as the source image and that it's not antialiased. Setting up the Graphics is always the same, so you can move it into a function:

makeMask[img_, shapes_] := 
 Rasterize[
  Style[Graphics[{White, shapes}, 
    PlotRange -> Transpose[{{0, 0}, ImageDimensions[img]}], 
    ImageSize -> ImageDimensions[img], Background -> Black], 
   Antialiasing -> False]]

mask = makeMask[img, Rectangle[{30, 150}, {230, 210}]]

And then just add that to the source image:

img + mask

enter image description here

$\endgroup$
3
$\begingroup$
ImageApply[# + Unitize @ Most @ #2 &, {img, ImageAlign[img, iTrim]}] (* or *)
ImageAdd[img, ImageApply[Unitize @ Most @ # &, ImageAlign[img, iTrim]]] (* or *)
ImageAdd[img, ImageMultiply[ImageAlign[img, iTrim, Background -> Black], 999]]

enter image description here

$\endgroup$
  • $\begingroup$ I guess we both thought of ImageAlign at the same time... (+1) $\endgroup$ – Jens Jul 10 '18 at 5:54
  • $\begingroup$ @Jens, thank you. Much cleaner version with ImageSubtract doesn't work in v9 if the number of channels are unequal (or one of the images is single-channel.) $\endgroup$ – kglr Jul 10 '18 at 6:00
1
$\begingroup$

I just found a clumsy solution. There may be a much more efficient one.

img = ExampleData[{"TestImage", "House"}]
loc = {{x1, y1}, {x2, y2}} = {{30, 150}, {230, 210}};
iTrim = ImageTrim[img, loc]

{w, h} = ImageDimensions@img
iTrimM = ImageMultiply[iTrim, 999]
iPad = ImagePad[iTrimM, {{x1, w - x2}, {y1, h - y2}}]
ImageAdd[img, iPad]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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