机器视觉入门之路(二四,旋转矩形截取图像,c#)

加载一幅图像:

好,现在取线图像40组(注意对比前头:已经调整成40组横向线图像):

 //glob_buffer8整幅图
            List<PosAndGrey> 线图像40 = new List<PosAndGrey>();
            for (int i = 0; i < 40; i++)//40条线,
            {
                PosAndGrey cc = new PosAndGrey();
                cc.tempgrey = new List<float>();
                cc.position = new List<PointF>();
                Size wwhh = new Size(pictureBox1.Image.Width, pictureBox1.Image.Height);
                获取线图像(glob_buffer8, 一众线[i], ref cc, wwhh);
                线图像40.Add(cc);
            }
            //显示截取的线图像ww*hh
            int hh = 40;
            int ww = 线图像40[0].tempgrey.Count;          
            byte[] 截取旋转图像 = new byte[ww * hh * 4];
            for (int i = 0; i < hh; i++)//h
                for (int j = 0; j < ww; j++)//w
                {
                    int temp = (i * ww + j) * 4;
                    截取旋转图像[temp] = (byte)((int)线图像40[i].tempgrey[j]);
                    截取旋转图像[temp + 1] = (byte)((int)线图像40[i].tempgrey[j]);
                    截取旋转图像[temp + 2] = (byte)((int)线图像40[i].tempgrey[j]);
                    截取旋转图像[temp + 3] = 255;                 
                }

效果:我们以后称呼他,线扫描稀疏图(想起激光线扫描图像,真是异曲同工)。

这里边出现两个不曾见

第一,   public struct PosAndGrey
        {
         public   List<float> tempgrey ;
         public List<PointF> position ;
        }

第二,public void   获取线图像(byte[] buffer8, mg_line mgl, ref PosAndGrey pAg, Size wh)
        {
            float x, y;
            float dx, dy, k;
      
            PointF Start = mgl.pt_start; 
            PointF End=mgl.pt_end;
            dx = (float)(End.X - Start.X);
            dy = (float)(End.Y - Start.Y);
            k = dy / dx;
            if (Math.Abs(dx) < 0.001)
            {
                k = 65535;//给定一个最大值
            }
            x = Start.X;
            if (Start.Y < 0) Start.Y = 0;
            y = Start.Y;
            
            #region xiaoyu1
            if (Math.Abs(k) < 1)
            {
                if (x > End.X)
                {
                    pAg.position.Clear();
                    pAg.tempgrey.Clear();
                    for (; x >= End.X; x--)
                    {
                        float j = y;
                        int i = (int)(x);
                        float tempf = Math.Abs(j - (int)j);
                        PointF tempPt = new PointF(i, j);
                        float grey = 0;
                        if (k > -1 && k <= 0)
                        {
                            float avgGrey = (float)buffer8[(int)j * wh.Width + i];
                            float avgGrey1 = (float)buffer8[((int)j + 1) * wh.Width + i];
                            grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
                        }
                        else
                        {
                            float avgGrey = (float)buffer8[(int)j * wh.Width + i];
                            float avgGrey1 = (float)buffer8[((int)j - 1) * wh.Width + i];
                            grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
                        }
                   
                        pAg.tempgrey.Add(grey);
                        pAg.position.Add(tempPt);
                        y = y - k;
                    }            
                }
                else
                {
        
                    pAg.position.Clear();
                    pAg.tempgrey.Clear();
                    for (; x <= End.X; x++)
                    {
                        float j = y;
                        int i = (int)(x);
                        float tempf = Math.Abs(j - (int)j);
                        PointF tempPt = new PointF(i, j);
                        float grey = 0;
                        if (k < 1 && k >= 0)
                        {
                            float avgGrey = (float)buffer8[(int)j * wh.Width + i];
                            float avgGrey1 = (float)buffer8[((int)j + 1) * wh.Width + i];
                            grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
                        }
                        else
                        {
                            float avgGrey = (float)buffer8[(int)j * wh.Width + i];
                            float avgGrey1 = (float)buffer8[((int)j - 1) * wh.Width + i];
                            grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
                        }
              
                        pAg.tempgrey.Add(grey);
                        pAg.position.Add(tempPt);
                        y = y + k;
                    }
             
                }
            }

            #endregion xiaoyu1
            #region dayu1
            if (Math.Abs(k) >= 1)
            {

                if (y > End.Y)
                {    
                    pAg.position.Clear();
                    pAg.tempgrey.Clear();
                    for (; y >= End.Y; y--)
                    {
                        int j = (int)(y);
                        float i = x;

                        float tempf = Math.Abs(i - (int)i);
                        PointF tempPt = new PointF(i, j);
                        float grey = 0;

                        if (k > 1)
                        {
                            float avgGrey = (float)buffer8[j * wh.Width + (int)i];
                            float avgGrey1 = (float)buffer8[j * wh.Width + (int)i - 1];
                            grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
                        }
                        else
                        {
                            float avgGrey = (float)buffer8[j * wh.Width + (int)i];
                            float avgGrey1 = (float)buffer8[j * wh.Width + (int)i + 1];
                            grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
                        }                   
                        pAg.tempgrey.Add(grey);
                        pAg.position.Add(tempPt);
                        x = x - 1 / k;
                    }               
                }
                else
                {                
                    pAg.position.Clear();
                    pAg.tempgrey.Clear();
                    for (; y <= End.Y; y++)
                    {
                        int j = (int)(y);
                        float i = x;

                        PointF tempPt = new PointF(i, j);
                        float tempf = Math.Abs(i - (int)i);

                        float grey = 0;
                        if (k < -1)
                        {
                            float avgGrey = (float)buffer8[j * wh.Width + (int)i];
                            float avgGrey1 = (float)buffer8[j * wh.Width + (int)i - 1];
                            grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
                        }
                        else
                        {
                            float avgGrey = (float)buffer8[j * wh.Width + (int)i];
                            float avgGrey1 = (float)buffer8[j * wh.Width + (int)i + 1];
                            grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
                        }
                    
                        pAg.tempgrey.Add(grey);
                        pAg.position.Add(tempPt);
                        x = x + 1 / k;
                    }                  
                }
            }
            #endregion dayu1       
        }

最后这个函数,是前面博客中出现过,改过来的。

好,今天尝试成功。

为了截取这幅图,矩形框平移了好几次,旋转也不方便,能否用鼠标拖动呢?他们称之为橡皮筋技术,下一节讲....

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值