Problem with PixelSetColor

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
edsonlp1
Posts: 7
Joined: 2011-06-06T02:50:18-07:00
Authentication code: 8675308

Problem with PixelSetColor

Post by edsonlp1 »

Hi guys! I'm trying to make an average of set of images, making the average of the pixels, but it doesnt work to me.

This is my code:

Code: Select all

	for (int i=0; i<1; i++)
	{
		for (int j=0; j< width; j++)
		{
			//pixels=PixelGetNextIteratorRow(iterator,&numberwands);
			for( int k=0; k < height ; k++)
			{

				status= MagickGetImagePixelColor(mw,j,k,pw);
				//pixell[0] = PixelGetColorAsString(pw);

				rojo1 = PixelGetRed(pw);
				verde1 = PixelGetGreen(pw);
				azul1 = PixelGetBlue(pw);
				status= MagickGetImagePixelColor(mw1,j,k,pw1);
				rojo2 = PixelGetRed(pw1);
				verde2 = PixelGetGreen(pw1);
				azul2 = PixelGetBlue(pw1);
				rojo = (rojo1+rojo2)/2;
				verde = (verde1+verde2)/2;
				azul = (azul1+azul2)/2;
				PixelSetRed(pw1,rojo);
				PixelSetGreen(pw1,verde);
				PixelSetBlue(pw1,azul);
				
			}
		}	//PixelSyncIterator(iterator);
		MagickNextImage(mw);
		
	}
I can read every RGB pixel, even operate with them, but the PixelSetRed, PixelSetGreen, PixelSetBlue doesn't work.. What i'm doing wrong?

Thanks for your help! :D
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Problem with PixelSetColor

Post by el_supremo »

PixelSetRed, PixelSetGreen and PixelSetBlue do not set a pixel in an image. They only change the PixelWand itself.
You need to use pixel iterators to access and modify the pixelwands and then use PixelSyncIterator to write them back into the image.
See my example here which uses iterators:
http://members.shaw.ca/el.supremo/Magic ... dulate.htm

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
edsonlp1
Posts: 7
Joined: 2011-06-06T02:50:18-07:00
Authentication code: 8675308

Re: Problem with PixelSetColor

Post by edsonlp1 »

Thank you Pete.. I've done finally using this code.

Code: Select all


	for (int i=0; i<5; i++) //NUMBER OF IMAGES
	{
	iterator1=NewPixelIterator(mw);
	iterator2 = NewPixelIterator (mw1);
		for (int j=0; j<height; j++)
		{
					
			pixels1=PixelGetNextIteratorRow(iterator1,&numberwands);
			pixels2=PixelGetNextIteratorRow(iterator2,&numberwands);

			for( int k=0; k < width; k++)
			{
				PixelGetMagickColor(pixels1[k],&pixel1);
				PixelGetMagickColor(pixels2[k],&pixel2);
				/*AVERAGE*/
				
				pixelR.red=((pixel1.red + pixel2.red)/2/255);
				pixelR.green= ((pixel1.green + pixel2.green)/2/255);
				pixelR.blue= ((pixel1.blue + pixel2.blue)/2/255);
				

				/* CONVIERTO A FORMATO RGB(XXX,XXX,XXX)*/
				sprintf(conv,"%.2f",pixelR.red);
				strcpy(buff,"RGB(");
				strcat(buff,conv);
				strcat(buff,",");
				
				sprintf(conv,"%.2f",pixelR.green);
				strcat(buff,conv);
				strcat(buff,",");
				
				sprintf(conv,"%.2f",pixelR.blue);
				strcat(buff,conv);
				strcat(buff,")");

				/* IMPRIMO*/
				status = PixelSetColor(pixels2[k],buff);

			}
				PixelSyncIterator(iterator1);
				PixelSyncIterator(iterator2);
		}	
		DestroyPixelIterator(iterator1);
		DestroyPixelIterator(iterator2);
		MagickNextImage(mw);
		MagickResetIterator(mw1);
		printf(".");
	}
	
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Problem with PixelSetColor

Post by el_supremo »

You don't need to use PixelSetColor. Your inner loop should work like this:

Code: Select all

         for( int k=0; k < width; k++)
         {
            PixelGetMagickColor(pixels1[k],&pixel1);
            PixelGetMagickColor(pixels2[k],&pixel2);

            /*AVERAGE*/
            PixelSetRed(pixels2[k],(pixel1.red + pixel2.red)/2/255);
            PixelSetGreen(pixels2[k],(pixel1.green + pixel2.green)/2/255);
            PixelSetBlue(pixels2[k],(pixel1.blue + pixel2.blue)/2/255);
         }
and I suspect that you only need to sync iterator2 because pixels1[] hasn't been modified.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
edsonlp1
Posts: 7
Joined: 2011-06-06T02:50:18-07:00
Authentication code: 8675308

Re: Problem with PixelSetColor

Post by edsonlp1 »

Thanks Pete, but it doesn't work to me that procedure.

The RGB values are not working fine. I'll put my code here

Code: Select all

for (int j=0; j<height; j++)
		{
					
			pixels1=PixelGetNextIteratorRow(iterator1,&numberwands);
			pixels2=PixelGetNextIteratorRow(iterator2,&numberwands);

			for( int k=0; k < width; k++)
			{
				PixelGetMagickColor(pixels1[k],&pixel1);
				PixelGetMagickColor(pixels2[k],&pixel2);
				
			PixelSetRed(pixels2[k],((pixel1.red + pixel2.red)/2)/255);
                        PixelSetGreen(pixels2[k],((pixel1.green + pixel2.green)/2)/255);
                        PixelSetBlue(pixels2[k],((pixel1.blue + pixel2.blue)/2)/255);
				
			}
				PixelSyncIterator(iterator1);
				PixelSyncIterator(iterator2);
		}	
		DestroyPixelIterator(iterator1);
		DestroyPixelIterator(iterator2);
		MagickNextImage(mw);
		MagickResetIterator(mw1);
		printf(".");
Thank you again
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Problem with PixelSetColor

Post by el_supremo »

Sorry, this should work and it removes the need for any MagickPixelPackets :

Code: Select all

         for( int k=0; k < width; k++)
         {
            /*AVERAGE*/
            PixelSetRed(pixels2[k],(PixelGetRed(pixels1[k]) + PixelGetRed(pixels2[k]))/2);
            PixelSetGreen(pixels2[k],(PixelGetGreen(pixels1[k]) + PixelGetGreen(pixels2[k]))/2);
            PixelSetBlue(pixels2[k],(PixelGetBlue(pixels1[k]) + PixelGetBlue(pixels2[k]))/2);
         }
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
edsonlp1
Posts: 7
Joined: 2011-06-06T02:50:18-07:00
Authentication code: 8675308

Re: Problem with PixelSetColor

Post by edsonlp1 »

Thank you very much Pete!!

Works perfect!!
Post Reply