机器视觉入门之路(十二,初次接触图像放大缩小,c++)

下图是0.67x,0.67y的图像缩放:

当界面想多放几张图,有时图像太大,界面窗口放不下,就会有孙大圣绣花针的想法:

void ZoomNormal(BYTE * image0,BYTE * &image1,unsigned int w,unsigned int h,
                         unsigned int & outwidth,unsigned int & outheight,double ZoomX,double ZoomY)
{
    outwidth=(int)(w*ZoomX+0.5);
    outheight=(int)(h*ZoomY+0.5);

    int newbuffersize=outwidth*outheight*4;
    image1=new BYTE[newbuffersize];
    memset(image1,255,newbuffersize);

    BYTE *copypixel=NULL;
    BYTE *objpixel=NULL;

    int x=0;
    int y=0;
    int tempY;
    int tempJ;

    for(int j=0;j<outheight;j++)
    {
        y=(int)(j/ZoomY+0.5);
        if(y>=h) y--;

        tempY=y*w*4;
        tempJ=j*outwidth*4;
        for(int i=0;i<outwidth;i++)
        {
            x=(int)(i/ZoomX+0.5);
            if(x>=w)
                x--;
            copypixel=image0+tempY+x*4;
            objpixel=image1+tempJ+i*4;
            memcpy(objpixel,copypixel,4);
        }
    }
其实除了图像的缩放,图像为了适应窗口,也是需要作出改变的,在此基础上,你能否把他改为有自适应窗口的功能呢?

这也是跟随(following)鼠标自由缩放的基础(博客中已存在),从这里,也隐含了图像尺度的概念,再如,图像金字塔的实现等等。

图像处理,要求速度高,你能否在这个算法上提高速度呢?

缩小会失去像素,放大需要插值,对于图像处理,都是比较谨慎的,因为原图像才是蓝本。

补充说明(202009281719):

以上代码是32位图像的缩放,以下是8位图像的缩放,c#代码,做一对比

  void ZoomNormal(byte[] image0, ref byte[] image1, int w, int h,
                       ref  int outwidth, ref int outheight, double ZoomX, double ZoomY)
        {
            outwidth = (int)(w * ZoomX + 0.5);
            outheight = (int)(h * ZoomY + 0.5);
         
            int newbuffersize = outwidth * outheight ;
            image1 = new byte[newbuffersize];        

            int x = 0;
            int y = 0;
            int tempY;
            int tempJ;

            for (int j = 0; j < outheight; j++)
            {
                y = (int)(j / ZoomY + 0.5);
                if (y >= h) y--;

                tempY = y * w ;
                tempJ = j * outwidth ;
                for (int i = 0; i < outwidth; i++)
                {
                    x = (int)(i / ZoomX + 0.5);
                    if (x >= w)
                        x--;
                   
                    image1[tempJ + i ] = image0[tempY + x ];
                  
                }
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值