在撰写图像处理领域相关的论文中通常需要将图片的某个小块进行放大,以便论文审稿者看到图像清晰的细节信息,本文主要使用matlab和QT(需要搭建opencv)来实现此种效果。
function [ dest ] = draw_rect(src, startPosition, rectSize, lineSize, color)
[start_y, start_x] = deal(startPosition(1),startPosition(2)); % start position
[rect_size_w, rect_size_h] = deal(rectSize(1),rectSize(2)); % Rect Size
[width, height, channel] = size(src);
if channel==1
dest(:, : ,1) = src;
dest(:, : ,2) = src;
dest(:, : ,3) = src;
else
dest = src;
end
for channel_i = 1 : 3
for line_i = 1 : lineSize % expand inside
gain = line_i-1;
dest(start_y+gain, (start_x+gain):(start_x+rect_size_w-gain), channel_i ) = color(channel_i); %above
dest(start_y+rect_size_h-gain, (start_x+gain):(start_x+rect_size_w-gain), channel_i ) = color(channel_i); %bottom
dest((start_y+gain):(start_y+rect_size_h-gain), start_x+gain, channel_i ) = color(channel_i); %left
dest((start_y+gain):(start_y+rect_size_h-gain), start_x+rect_size_w-gain, channel_i ) = color(channel_i); %right
end
end
void MainWindow::on_pushButton_2_clicked()
{
int start_h = 220, start_w = 180;
int rectSize = 50, scaleSize=3;
Rect rect1 = Rect(start_h, start_w, rectSize, rectSize); // specific rect
Mat srcImage = imread("/home/mark/Sofeware/Qt/Qt_Project/QtApplication2/QT_DrawRect/lena.png");
Mat imageRect = srcImage;
cvtColor(imageRect, imageRect, CV_BGR2RGB); // convert the BGR image to RGB
Mat image_ROI = imageRect(rect1); // define the ROI area and magnification ROI area
Mat image_scaleROI;
cv::resize(image_ROI, image_scaleROI, Size(scaleSize*rectSize, scaleSize*rectSize));
Rect rect2 = Rect(imageRect.rows-image_scaleROI.rows, imageRect.cols-image_scaleROI.cols, // magnification rect
image_scaleROI.rows, image_scaleROI.cols);
image_scaleROI.copyTo(imageRect(rect2)); // cover the scaled ROI area
rectangle(imageRect, rect1, cvScalar(255,0,0),2); // draw two rect
rectangle(imageRect, rect2, cvScalar(255,0,0),2);
QImage image((const uchar*)imageRect.data, imageRect.cols, imageRect.rows, // show image in Qlabel
imageRect.cols*imageRect.channels(), QImage::Format_RGB888);
QImage showImage = image.scaled(ui->label->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(QPixmap::fromImage(showImage));
cvtColor(imageRect, imageRect, CV_RGB2BGR);
imwrite("/home/mark/Sofeware/Qt/Qt_Project/QtApplication2/QT_DrawRect/output.png", imageRect);
}
最终应用两种方法得到的测试结果为:
完整的代码工程文件请前往github下载
https://github.com/RussellEven/Draw_Rect
转载须知:https://blog.csdn.net/weixin_39444746/article/details/88763550