What's the fastest method for accessing to pixel color?

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

What's the fastest method for accessing to pixel color?

Post by Airon65 »

Now I use following approach for accessing to image pixel colors:

Code: Select all

unsigned short _imgWidth = 1920, _imgHeight = 1080;

Magick::Image *image = new Magick::Image();
image->size( Magick::Geometry( _imgWidth, _imgHeight ) );
image->backgroundColor( Magick::Color( "white" ) );
image->erase();

for ( unsigned int j = 0; j < _imgHeight; j++ ) {
	for ( unsigned int i = 0; i < _imgWidth; i++ ) {
		Magick::Color c = image->pixelColor( i, j );
		unsigned char r = c.quantumRed()	* 255 / QuantumRange;
		unsigned char g = c.quantumGreen()	* 255 / QuantumRange;
		unsigned char b = c.quantumBlue()	* 255 / QuantumRange;		
	}	
}

delete image;
I should to process about 5000 images like that. It takes me about 3-4 seconds to just get all the pixels of one image (1920x1080). Are there any methods which work a little bit faster than that? :)

I use ImageMagick 7.0.5-2 Q16 x86_64 2017-03-11.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: What's the fastest method for accessing to pixel color?

Post by magick »

You likely want to get one row of pixels at a time with:

Code: Select all

  Pixels view(image);
  for ( unsigned int j = 0; j < _imgHeight; j++ ) {
    Quantum *pixels = view.getConst(0,y,_imgWidth,1)
    for ( unsigned int i = 0; i < _imgWidth; i++ ) {
      unsigned char r = 255*QuantumScale**pixels++;
      unsigned char g = 255*QuantumScale**pixels++;
      unsigned char b = 255*QuantumScale**pixels++;
    }
  
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: What's the fastest method for accessing to pixel color?

Post by Airon65 »

Thanks! That approach works really fast!
Last edited by Airon65 on 2017-04-02T10:54:31-07:00, edited 1 time in total.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: What's the fastest method for accessing to pixel color?

Post by dlemstra »

I would advise you to do some more reading on how to code C++. The error that you are receiving should not be that difficult to figure out. This is an error that is not related to Magick++.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: What's the fastest method for accessing to pixel color?

Post by Airon65 »

Is that right that in case of my RGB image:

Code: Select all

Magick::Image *image = new Magick::Image();
image->size( Magick::Geometry( _imgWidth, _imgHeight ) );
image->backgroundColor( Magick::Color( "white" ) );
image->erase();
I get 4 bytes per pixel:

Code: Select all

Magick::Pixels view( *image );
for ( unsigned int j = 0; j < _imgHeight; j++ ) {
	const Magick::Quantum *pixels = view.getConst( 0, j, _imgWidth, 1 );
	for ( unsigned int i = 0; i < _imgWidth; i++ ) {
		unsigned int r = 255 * QuantumScale * *( pixels + ( i * 4 ) + 0 );
		unsigned int g = 255 * QuantumScale * *( pixels + ( i * 4 ) + 1 );
		unsigned int b = 255 * QuantumScale * *( pixels + ( i * 4 ) + 2 );
	}	
}
if I set: "( i * 3 )" - I get a broken image. I didn't why I try to test "i*4" but it worked for my as I thought RGB (3 bytes per pixel image).
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: What's the fastest method for accessing to pixel color?

Post by magick »

Use image->channels() to determine how many channels you have. If its four add an alpha channel.
Post Reply