how to do this code?
-
dear all how to do the following code, i spent much time, however i couldn't complete it, thanks a lot. for (int iY=0,iX=0;iY { if (iY==0) ucImgdata[iX]=ucImgdata[iX]*mask[1]+ucImgdata[iX+1]*mask[2]; else if (iY==iHeight-1) ucImgdata[iX]=ucImgdata[iX]*mask[0]+ucImgdata[iX+1]*mask[1]; else ucImgdata[iX]=ucImgdata[iX]*mask[0]+ucImgdata[iX+1]*mask[1]+ucImgdata[iX+2]*mask[2]; } ucImgdata+=iBuffwidth; actually my goal is i have two arrays, one is 1x3, the other is iWidth x iHeight,first i want to multiply every point of iWidth x iHeight by center point of 1x3, when first point or last point of 1x3 array is out of iWidth x iHeight, just don't evaluate.that means i multiply each point of iWidth x iHeight by center point of 1x3 from beginning to end. however i tried to do this, but couldn't, the code above is no problem when i compiled, but when i processed the image via invoking this function, it aborted, thanks a lot
Li Zhiyuan
-
dear all how to do the following code, i spent much time, however i couldn't complete it, thanks a lot. for (int iY=0,iX=0;iY { if (iY==0) ucImgdata[iX]=ucImgdata[iX]*mask[1]+ucImgdata[iX+1]*mask[2]; else if (iY==iHeight-1) ucImgdata[iX]=ucImgdata[iX]*mask[0]+ucImgdata[iX+1]*mask[1]; else ucImgdata[iX]=ucImgdata[iX]*mask[0]+ucImgdata[iX+1]*mask[1]+ucImgdata[iX+2]*mask[2]; } ucImgdata+=iBuffwidth; actually my goal is i have two arrays, one is 1x3, the other is iWidth x iHeight,first i want to multiply every point of iWidth x iHeight by center point of 1x3, when first point or last point of 1x3 array is out of iWidth x iHeight, just don't evaluate.that means i multiply each point of iWidth x iHeight by center point of 1x3 from beginning to end. however i tried to do this, but couldn't, the code above is no problem when i compiled, but when i processed the image via invoking this function, it aborted, thanks a lot
Li Zhiyuan
Hi, if i get what you're saying, you are trying to a convolution on the image. You have a convolution kernel of [3x1] ( width is 3, height is 1) and an image of [height x width]. I'll assume your image is an 8-bit per pixel image (Grayscale). You can achieve convolution by;
unsigned char kernel[3] = {1,3,1}; // kernel values (example) unsigned char *pImage = XXX; // pointer to the image for(int y = 0; y < height; y++) for(int x = 1; x < width-1; x++) // start from 2nd pixel, and stop 1 before width becoz of kernel size { pImage[x+y*width] = pImage[(x-1)+y*width]*kernel[0] + pImage[x+y*width]*kernel[1] + pImage[(x+1)+y*width]*kernel[2]; }
Also note, the result might be bigger than the capacity of unsigned char (in this example), so it is advisable to divide the convolution result by the kernel total size, that is (1+3+1) = 5 in this case. (Not shown above). If you want to maintain the result without dividing, the output image size must be bigger, like 'int' etc. Hope this helps. Before i forget, the kernel i used is [3x1], you can also do [1x3] vertical kernel, just change the x,y operation above. Cheers -
Hi, if i get what you're saying, you are trying to a convolution on the image. You have a convolution kernel of [3x1] ( width is 3, height is 1) and an image of [height x width]. I'll assume your image is an 8-bit per pixel image (Grayscale). You can achieve convolution by;
unsigned char kernel[3] = {1,3,1}; // kernel values (example) unsigned char *pImage = XXX; // pointer to the image for(int y = 0; y < height; y++) for(int x = 1; x < width-1; x++) // start from 2nd pixel, and stop 1 before width becoz of kernel size { pImage[x+y*width] = pImage[(x-1)+y*width]*kernel[0] + pImage[x+y*width]*kernel[1] + pImage[(x+1)+y*width]*kernel[2]; }
Also note, the result might be bigger than the capacity of unsigned char (in this example), so it is advisable to divide the convolution result by the kernel total size, that is (1+3+1) = 5 in this case. (Not shown above). If you want to maintain the result without dividing, the output image size must be bigger, like 'int' etc. Hope this helps. Before i forget, the kernel i used is [3x1], you can also do [1x3] vertical kernel, just change the x,y operation above. Cheersthanks friend, my kernel is also 3x1, i made a mistake. thanks a lot, first let me try to do, if any problem, hope you can continue helping me.thanks freind, i need to do twice convolution, first time is 1x3, sencond is 3x1, by i have to multiply each point of image, i read your code, you started from second pixel of image, my boss asked me to start from first pixel. how to carry out twice convolution? first 1x3, and then 3x1, thanks a lot
Li Zhiyuan
modified on Tuesday, March 4, 2008 1:56 AM
-
thanks friend, my kernel is also 3x1, i made a mistake. thanks a lot, first let me try to do, if any problem, hope you can continue helping me.thanks freind, i need to do twice convolution, first time is 1x3, sencond is 3x1, by i have to multiply each point of image, i read your code, you started from second pixel of image, my boss asked me to start from first pixel. how to carry out twice convolution? first 1x3, and then 3x1, thanks a lot
Li Zhiyuan
modified on Tuesday, March 4, 2008 1:56 AM
-
dear all how to do the following code, i spent much time, however i couldn't complete it, thanks a lot. for (int iY=0,iX=0;iY { if (iY==0) ucImgdata[iX]=ucImgdata[iX]*mask[1]+ucImgdata[iX+1]*mask[2]; else if (iY==iHeight-1) ucImgdata[iX]=ucImgdata[iX]*mask[0]+ucImgdata[iX+1]*mask[1]; else ucImgdata[iX]=ucImgdata[iX]*mask[0]+ucImgdata[iX+1]*mask[1]+ucImgdata[iX+2]*mask[2]; } ucImgdata+=iBuffwidth; actually my goal is i have two arrays, one is 1x3, the other is iWidth x iHeight,first i want to multiply every point of iWidth x iHeight by center point of 1x3, when first point or last point of 1x3 array is out of iWidth x iHeight, just don't evaluate.that means i multiply each point of iWidth x iHeight by center point of 1x3 from beginning to end. however i tried to do this, but couldn't, the code above is no problem when i compiled, but when i processed the image via invoking this function, it aborted, thanks a lot
Li Zhiyuan
Step through the code using the debugger.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne