首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像中感兴趣区域(ROI)绘制的优化

图像中感兴趣区域(ROI)绘制的优化
EN

Stack Overflow用户
提问于 2011-09-03 17:02:42
回答 3查看 2.9K关注 0票数 0

我通过将矩形区域外的区域变暗来绘制矩形区域,如此图像http://www.codeproject.com/script/Membership/Uploads/4613314/roi.jpg所示

但是image.MakeTransparent花费了太多的时间。提高绘图速度的最好方法是什么?

代码语言:javascript
复制
void DrawRoi(Bitmap Image, RectangleF rect)
{
        Rectangle roi = new Rectangle();

        roi.X = (int)((float)Image.Width * rect.X);
        roi.Y = (int)((float)Image.Height * rect.Y);
        roi.Width = (int)((float)Image.Width * rect.Width);
        roi.Height = (int)((float)Image.Height * rect.Height);

        Stopwatch timer = new Stopwatch();
        timer.Start();
        // graphics manipulation takes about 240ms on 1080p image
        using (Bitmap roiMaskImage = CreateRoiMaskImage(ImageWithRoi.Width, ImageWithRoi.Height, roi))
        {
                using (Graphics g = Graphics.FromImage(ImageWithRoi))
                {                                
                        g.DrawImage(Image, 0, 0);
                        g.DrawImage(roiMaskImage, 0, 0);
                        Pen borderPen = CreateRoiBorderPen(ImageWithRoi);
                        g.DrawRectangle(borderPen, roi);
                }
        }
        Debug.WriteLine("roi graphics: {0}ms", timer.ElapsedMilliseconds);
        this.imagePictureBox.Image = ImageWithRoi;
}

Bitmap CreateRoiMaskImage(int width, int height, Rectangle roi)
{
        Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        using (Graphics g = Graphics.FromImage(image))
        {
                SolidBrush dimBrush = new SolidBrush(Color.FromArgb(64, 0, 0, 0));
                g.FillRectangle(dimBrush, 0, 0, width, height);
                SolidBrush roiBrush = new SolidBrush(Color.Red);
                g.FillRectangle(roiBrush, roi);
                image.MakeTransparent(Color.Red);
                return image;
        }                
}

Pen CreateRoiBorderPen(Bitmap image)
{
        float width = ((float)(image.Width + image.Height) * 2.5f) / (float)(640 + 480);
        if (width < 1.0f)
                width = 1.0f;
        Pen pen = new Pen(Color.FromArgb(255, 0, 255, 0), width);
        return pen;
}
EN

回答 3

Stack Overflow用户

发布于 2011-09-05 16:29:22

根本不要操控图像。只需在您的图像顶部绘制“调光”即可。您可以实现相同的效果,例如,通过

1)在整个图像上绘制一个大的半透明区域,并将剪辑区域设置为您的ROI

代码语言:javascript
复制
// Assume for simplicity the image is size w*h, and the graphics is the same size. 
// The ROI is a rectangle named roiRect.
g.DrawImageUnscaled(image, 0, 0 , w, h);
g.SetClip(roiRect);
g.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.Black)), 0, 0, w, h);
g.ResetClip();    

2)绘制图像,然后绘制调光矩形,然后在其上绘制图像的roi部分。

3)绘制4个独立的矩形顶部/右侧/左侧/底部,以省略您的ROI。

票数 2
EN

Stack Overflow用户

发布于 2011-09-05 16:20:41

我完全看不出调用.MakeTransparent有什么意义。当您创建蒙版图像时

代码语言:javascript
复制
Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);

它已经是透明的了。最好将dimBrush绘制为四个独立的矩形(ROI上方/下方/左侧/右侧),避免在已经透明的区域中绘制!

票数 1
EN

Stack Overflow用户

发布于 2012-03-20 22:15:22

看一下WriteableBitmap类。在WritePixels方法中,你可以用一个Int32Rect定义一个感兴趣区域。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7292168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档