[SOLVED]Writing sub-images From an image with 50% overlappin

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
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

[SOLVED]Writing sub-images From an image with 50% overlappin

Post by rohitkrishna »

hi everyone..

Im using image magick for a quite some time and im stuck at a point now...can any one please hep how to output different subimages from a big images with 50% overlapping using magick wand and c language. I maintain the r,g,b values of big image in a big 1D array and i have to write the output to different sub-images with 50% overlapping. i figured how to write mutiple sub-images from a big 1D array seperately but im stuck at 50% overlapping. im including my code on how to write different multiple sub-images seperately with no overlapping. Can anyone please help me in this.

Code: Select all

for(int ip=0;ip<v.img_cnt;ip++) // img_cnt is already predefined
	{
		MagickWand    *image;
		MagickBooleanType status;
		MagickWandGenesis(); // to initialize the magick-wand Environment
		image=NewMagickWand(); // To create the wand to store image
		MagickSetSize(image,v.width,v.height);
		status = MagickReadImage(image, "xc:none" );
		if (status == MagickFalse)
			printf ("status problem");
		PixelIterator *new_iterator;
		PixelWand **new_pixels;
		a=0;
		while(a<v.n_sbimg)
		{
			new_iterator=NewPixelRegionIterator(image,v.col[a],v.row[a],v.sbimg_wdt,v.sbimg_hgt);
			for(y=0;y<v.sbimg_hgt;y++)
			{
				new_pixels = PixelGetNextIteratorRow(new_iterator, &v.sbimg_wdt);
				for(x=0;x<v.sbimg_wdt;x++)
				{
					int t=loc(y,x,v.sbimg_wdt)+ a*v.sbimg_pxl+ip*(v.width*v.height*3);    //calculates position of r,g,b values in the 1D array
					unsigned char r=new_data[t],g=new_data[t+1],b=new_data[t+2];       //new_data contains new image r,g,b values
					char color[64];				
					sprintf(color,"rgb(%d,%d,%d)",r,g,b);
					PixelSetColor(new_pixels[x],color); // this function sets the color of pixel.
					PixelSyncIterator(new_iterator);
				}
			}
		a++;
		}		
Last edited by rohitkrishna on 2012-05-19T11:16:31-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Writing sub-images From an image with 50% overlapping

Post by anthony »

Best idea from an API is to read the image width/height and work out the overlapping crop areas yourself.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Writing sub-images From an image with 50% overlapping

Post by rohitkrishna »

anthony wrote:Best idea from an API is to read the image width/height and work out the overlapping crop areas yourself.
Thank you..But im not using command line...i want to program it..using c and magickwand.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Writing sub-images From an image with 50% overlapping

Post by fmw42 »

I believe that Anthony was saying that you can do that from an API like Magickwand. You just have to read the image size, compute the subsections taking into account the overlap and then crop them using Magickwand.
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Writing sub-images From an image with 50% overlapping

Post by rohitkrishna »

fmw42 wrote:I believe that Anthony was saying that you can do that from an API like Magickwand. You just have to read the image size, compute the subsections taking into account the overlap and then crop them using Magickwand.
Can u give me an example please...im new to imagemagick..
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Writing sub-images From an image with 50% overlapping

Post by fmw42 »

Sorry I do not know Magickwand API. I only use the command line commands in bash shell scripting.

The basic idea is:
1) find the image width and height to use as loop end coordinates (less the crop width and height)
2) decide how big you want your crop area in width and height (w and h)
3) compute the half width and half height of the crop dimensions to use as offsets
4) Run a loop to crop at those widths and heights starting at Xoffset=0 and incrementing over the Xoffset for the crop by the half width, ie w/2
5) At the end of the row, start over in Xoffset=0 but increase the Yoffset by h/2 and increment the Xoffset by w/2 again
6) Repeat until at the bottom of the image.
Last edited by fmw42 on 2012-05-18T15:58:59-07:00, edited 1 time in total.
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Writing sub-images From an image with 50% overlapping

Post by rohitkrishna »

fmw42 wrote:Sorry I do not know Magickwand API. I only use the command line commands in bash shell scripting.

The basic idea is:
1) find the image width and height to use as loop end coordinates (less the crop width and height)
2) decide how big you want your crop area in width and height (w and h)
3) compute the half width and half height of the crop dimensions to use as offsets
4) Run a loop to crop at those widths and heights incrementing over the Xoffset=w/2 for the crop by the half width
5) At the end of the row, start over in Xoffset=0 but increase the Yoffset=h/2 and increment the Xoffset by w/2 again
6) Repeat until at the bottom of the image.
THank you Very much. i have solved the problem and now iam able to produce overlapping images.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [SOLVED]Writing sub-images From an image with 50% overla

Post by fmw42 »

I just posted a new script, overlapcrop, to my web site below. It allows the user to specify the amount of overlap desired and has several forms of output, such as animation and montage, in addition to each individual subsection.
Post Reply