图像感兴趣区域(ROI)绘制的优化
本文关键字:绘制 优化 ROI 感兴趣 区域 图像 | 更新日期: 2023-09-27 18:07:19
我绘制了矩形ROI区域,通过将其外部区域变暗,如下图http://www.codeproject.com/script/Membership/Uploads/4613314/roi.jpg
但是image.MakeTransparent
花费的时间太长。提高绘图速度的最佳方法是什么?
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;
}
不要对图片进行任何操作。只需在图像的顶部绘制"调光"。例如,您可以通过
实现相同的效果1)在整个图像上绘制一个大的半透明区域,剪辑区域设置为您的投资回报率
// 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
我根本看不出调用. maketransparent有什么意义。当你创建蒙版图像
Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
已经是透明的了。最好将dimBrush绘制为四个单独的矩形(ROI的上方/下方/左侧/右侧区域),并避免在已经透明的区域中绘制!
看一下WriteableBitmap类。在WritePixels方法中,你可以用Int32Rect定义ROI。