2
$\begingroup$

I want to assemble two gray scale images of 100*100 pixels.

  • From image two 30 pixels at the left border should be deleted and the remaining image shoud me shifted 10 pixels down.

  • After that image 1 and the modified image 2 should be assembled in row.


img1 = Image[Array[0.3 &, {100, 100}]]

enter image description here

img2 = Image[Array[0.8 &, {100, 100}]]

enter image description here

How can the following code be improved/replaced?

Instead of using ImageData (as I did it) I would like to know if a solution exists with image functions like ImageCompose or others.

xshift = 30;
yshift = 10;

img2Data = ImageData[img2];
img3Data = Array[0 &, {100, 100 - xshift}];

img3Data[[1 + yshift ;; 100, 1 ;; 100 - xshift]] = 
        img2Data[[1 ;; 100 - yshift, 1 + xshift ;; 100]];
assembledImage = ImageAssemble[{img1, Image[img3Data]}]

enter image description here

$\endgroup$
  • $\begingroup$ What is wrong with the code? Does it not do what you want? What improvements are you looking for? $\endgroup$ – MarcoB Jul 25 '17 at 20:16
  • $\begingroup$ @MarcoB: Instead of using ImageData (as I did it) I would like to know if a solution exists with image functions like ImageCompose or others, which would be much faster since I have to process thousands of image pairs this way (my original png images are of 1600*1200 pixels). $\endgroup$ – mrz Jul 26 '17 at 11:50
  • 3
    $\begingroup$ @mrz Have a look at ImageTake in the documentation. This does what you want on an Image level. $\endgroup$ – Thies Heidecke Jul 26 '17 at 12:20
2
$\begingroup$
Clear[trim]
trim[image_, xshift_, yshift_] := 
   ImageTrim[
      image,
      {{xshift + 1, yshift + 1}, ImageDimensions[image] + {-1, yshift - 1}}, 
      Padding -> Black
   ]

ImageAssemble[{img1, trim[img2, xshift, yshift]}]

Mathematica graphics

This works using Image* functions, but on my system it is not faster than your approach, rather surprisingly. However, I am not intimately familiar with the Image* functions, so perhaps a better solution exists.

$\endgroup$
  • $\begingroup$ thank you for the solution ... I will test it on my original images $\endgroup$ – mrz Jul 26 '17 at 15:40
  • 1
    $\begingroup$ Your solution is definitely faster than my code and a great improvement. For the comparison I used 100 gray scale png Images of 1600*1200 pixels, whereby xshift=95 and yshift=11. On my computer AbsoluteTiming of your solution gives about 50 sec and my code needs about 77 sec. $\endgroup$ – mrz Jul 27 '17 at 10:33
  • $\begingroup$ @mrz Glad to hear that, and thank you for the accept! $\endgroup$ – MarcoB Jul 27 '17 at 13:20

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.