Page 1 of 1

Remove repeated rows in image efficiently

Posted: 2013-02-19T18:20:44-07:00
by CompMan86
Hi all-

I have images that are very tall (~6000px tall by ~1000px wide) and I want to remove "boring parts" from them, where "boring parts" means segments where the same (or almost the same) rows of pixels are repeated more than 50 rows at a time. So for a simple example, if I had a 6000px tall image where the first 1000 rows had lots of text and shapes etc, and then the next 5000 rows were just (from left to right) 50 blue pixels followed by 950 red pixels) repeated row-after-row all the way down, I'd want to crop all but 50 of those rows.

Is there a clever/easy way of doing this with ImageMagick (or some other tool)?

If I were writing the algorithm (naively), I would do something like this:

Code: Select all

Current50rows = 2-dimensional array of pixel RGBs

while (not at end of image) {

   Next50Rows = 2-dimensional array of pixel RGBs

   differenceBetweenSlices = 0;

   For (rowNum = 0; rowNum < 50; rowNum++) {
      for (colNum = 0; colNum < 1000; colNum++) {
         //if the pixels are more than just slightly different, increment the "difference meter"
         if (abs( Current50Rows[rowNum,colNum] - Next50Rows[rowNum,colNum] ) < threshold) {
            differenceBetweenSlices++
         }
      }
   }
   //slices are basically the same
   if (differenceBetweenSlices < 50) {
      DeleteCurrent50RowsFromImage
   }
   
   Current50Rows = Next50Rows;
}
Thanks for your help!

Re: Remove repeated rows in image efficiently

Posted: 2013-02-19T18:50:11-07:00
by magick

Re: Remove repeated rows in image efficiently

Posted: 2013-02-19T19:38:26-07:00
by fmw42
Here are some tests I did a while back. http://www.fmwconcepts.com/misc_tests/l ... index.html

Note I do not think there is a delegate library for lqr for Windows. For Linux/Mac, you need to install the lqr delegate and then recompile IM adding --with-lqr in your ./configure command

Re: Remove repeated rows in image efficiently

Posted: 2013-02-20T11:29:11-07:00
by CompMan86
Thanks for the replies :-) I don't think Liquid Rescale is exactly what I need though... it seems like it works best on photos where slight distortions aren't a big deal, but I'll be using this on screenshots of computer UI (e.g., webpages) so I want to avoid any distortion to the "interesting" parts of the page. Also, I won't be specifying a target resolution -- I just want to automatically crop out "boring stuff", and if it turns out that the whole 6000 rows are interesting, I'm ok with it leaving the image alone. Also, I will need to run this on Windows...

Any other ideas? Or am I underestimating Liquid Rescale?

Thanks again!

Re: Remove repeated rows in image efficiently

Posted: 2013-02-22T18:18:58-07:00
by CompMan86
Anyone? Please? :-)

Re: Remove repeated rows in image efficiently

Posted: 2013-02-22T19:07:00-07:00
by snibgo
The obvious way is to write a program that reads an image. It scans the image, row by row. If it finds (x) rows that are identical (or nearly so), it removes all but (y) of those rows.

It you don't mind it being horibly slow, you could write a script that used IM to crop and compare adjacent rows.

Re: Remove repeated rows in image efficiently

Posted: 2013-02-26T17:46:45-07:00
by anthony
I did something like that in a script I called "de-pixelate"
http://www.imagemagick.org/Usage/scripts/de-pixelate

It is designed to remove duplicate rows and columns (to a limit count) so as to shrink 'box scaled' pixelated images back down to the ideal size.

NOTE; the script is rough, and old, and I have not used it in some time. but it did the job I needed it for.

PPS: for this situation I found that if you can determine the number of 'pixel boxes' that is present in the image then simply sampling the image with that size will also de-pixelate the image. Finding that count however is not a nice job.