Remove repeated rows in image efficiently

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
CompMan86
Posts: 3
Joined: 2013-02-19T17:17:37-07:00
Authentication code: 6789

Remove repeated rows in image efficiently

Post 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!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Remove repeated rows in image efficiently

Post by magick »

User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove repeated rows in image efficiently

Post 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
CompMan86
Posts: 3
Joined: 2013-02-19T17:17:37-07:00
Authentication code: 6789

Re: Remove repeated rows in image efficiently

Post 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!
CompMan86
Posts: 3
Joined: 2013-02-19T17:17:37-07:00
Authentication code: 6789

Re: Remove repeated rows in image efficiently

Post by CompMan86 »

Anyone? Please? :-)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove repeated rows in image efficiently

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Remove repeated rows in image efficiently

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply