QPixmap将绘制的正方形的内容复制到另一个图像中
QPixmap copy contents of a square drawn into another image
我有一个图像,我在上面画了一个矩形。之后,我尝试将矩形的内容复制到另一个 QLabel 上。这似乎有效,但是我似乎无法从图像的左上角开始对齐复制的图像。这是我正在做的
QPixmap original_image;
original_image.load("c:\Images\myimg.jpg");
original_image = original_image.scaled(ui.label->size().width(),ui.label->size().height());
//-----------------------------------------------------------------------
//Draw rectangle on this
QPixmap target_two(ui.label->size().width(),ui.label->size().height());
target_two.fill(Qt::transparent);
QPixmap target(ui.label->size().width(),ui.label->size().height());
target.fill(Qt::transparent);
QPainter painter(&target);
QPainter painter_two(&target_two);
QRegion r(QRect(0, 0, ui.label->size().width(), ui.label->size().height()), QRegion::RegionType::Rectangle); //Region to start copying
painter.setClipRegion(r);
painter.drawPixmap(0, 0, original_image); //Draw the original image in the clipped region
QRectF rectangle(x_start,y_start,clipRegion);
painter.drawRoundedRect(rectangle,0,0); //Last two parameters define the radius of the corners higher the radius more rounded it is
QRegion r_two(rectangle.toRect(), QRegion::RegionType::Rectangle);
painter_two.setClipRegion(r_two);
painter_two.drawPixmap(0,0,target);
ui.label->setPixmap(target);
ui.label_2->setPixmap(target_two);
底部图片是带有红色矩形的图像,这很好。上图是正方形内容的副本。唯一的问题是它不是从左上角开始的。
关于为什么我没有在左上角获得复制内容的任何建议。
逻辑中的问题是目标图像和target_two图像具有相同的大小 - 标签大小,并且您将复制的图像绘制在与初始标签中相同的位置。目前为止,一切都好。我将通过以下代码解决此问题:
[..]
// This both lines can be removed.
// QRegion r_two(rectangle.toRect(), QRegion::RegionType::Rectangle);
// painter_two.setClipRegion(r_two);
// Target rect. in the left top corner.
QRectF targetRect(0, 0, rectangle.width(), rectangle.height());
QRectF sourceRect(rectangle);
// Draw only rectangular area of the source image into the new position.
painter_two.drawPixmap(targetRect, target, sourceRect);
[..]
相关文章:
- 如何在 OpenCV 中将图像与另一个图像相关联
- 计算另一个图像像素满足条件的像素值的平均值
- 由于alpha值的原因,将一个图像混合到另一个图像会产生不正确的图像
- 访问和更改基于另一个图像的像素颜色-opencv c++
- 如何在打开另一个图像时关闭图像 - linux c ++
- 在 allegro5 中的另一个图像上的图像 (C++)
- 尝试在另一个 SDL2 上显示一个图像没有错误,但不会显示第二个图像
- 如何使用CIMG.H库在另一个图像上显示一个图像
- 如何在C 和QT中的另一个窗口中显示MainWindow的结果图像
- 在amd64体系结构上的C++中,将图像缓冲区blit到另一个缓冲区的xy偏移中的最快方法
- QPixmap将绘制的正方形的内容复制到另一个图像中
- 将图像指针传递到另一个程序
- 确保给定的图像被另一个图像翻转
- OpenCV将透明图像绘制到另一个图像上
- 给定尺寸为 26 * 7 的图像是否有可能在垫子的每一行中包含 78 个单独的颜色值,而另一个可以包含 77
- 如何使用opencv中的单应矩阵将一个点从一个图像重新投影到另一个图像
- 如何将一个图像叠加在另一个图像上?c++
- 将图像放在另一个图像上
- 是否有可能将一个图像放入另一个图像中,并将其大小调整到一定的尺寸
- 使用opencv将旋转和倾斜的图像插入到另一个图像的检测矩形中