Skip to content

Commit 0ebc08e

Browse files
Merge pull request BRL-CAD#29 from SP23-CSCE482/VisualCropping
Visual cropping
2 parents 44a9acf + 858e8d1 commit 0ebc08e

File tree

5 files changed

+83
-22
lines changed

5 files changed

+83
-22
lines changed

IFPainter.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,61 @@ void IFPainter::drawImageFitted(int x, int y, int width, int height, std::string
8484
resized_image.copyTo(destRoi);
8585
}
8686

87+
void IFPainter::drawDiagramFitted(int x, int y, int width, int height, std::string imgPath, std::string text)
88+
{
89+
y += 65;
90+
height -= 65;
91+
cv::Mat imageRaw = imread(imgPath, cv::IMREAD_UNCHANGED); // Load color image
92+
// Convert the image to grayscale for creating the mask
93+
cv::Mat gray_image;
94+
cv::cvtColor(imageRaw, gray_image, cv::COLOR_BGR2GRAY);
95+
// Create a mask of non-white pixels
96+
cv::Mat mask = gray_image < 255;
97+
// Find the bounding rectangle of non-white pixels
98+
cv::Rect bounding_rect = boundingRect(mask);
99+
// Crop the image to the bounding rectangle
100+
cv::Mat lilImage = imageRaw(bounding_rect);
101+
102+
103+
int imgWidth = lilImage.size().width;
104+
int imgHeight = lilImage.size().height;
105+
int heightOffset = 0;
106+
int widthOffset = 0;
107+
108+
if ((double)imgWidth / imgHeight > (double)width / height)
109+
{
110+
// image width is too large; bound on width
111+
int newHeight = (int)(width * (double)imgHeight / imgWidth);
112+
heightOffset = (height - newHeight) / 2;
113+
height = newHeight;
114+
}
115+
else
116+
{
117+
// image height is too large; bound on height
118+
int newWidth = (int)(height * (double)imgWidth / imgHeight);
119+
widthOffset = (width - newWidth) / 2;
120+
width = newWidth;
121+
}
122+
123+
cv::Mat resized_image;
124+
resize(lilImage, resized_image, cv::Size(width, height), cv::INTER_LINEAR);
125+
126+
//Trying to copyto the image on to the private image frame, img
127+
cv::Mat destRoi;
128+
try {
129+
destRoi = img(cv::Rect(x + widthOffset, y + heightOffset, resized_image.cols, resized_image.rows));
130+
}
131+
catch (...) {
132+
std::cerr << "Trying to create roi out of image boundaries" << std::endl;
133+
return;
134+
}
135+
resized_image.copyTo(destRoi);
136+
137+
// now, draw text and line
138+
this->drawLine(x + widthOffset, y + heightOffset - 5, x + widthOffset + width, y + heightOffset - 5, 5, cv::Scalar(0, 0, 0));
139+
this->drawTextCentered(x + widthOffset + width / 2, y + heightOffset - 65, 50, width, text, TO_BOLD);
140+
}
141+
87142
//Helper funciton to support getting the width of a text
88143
int IFPainter::getTextWidth(int height, int width, std::string text, int flags)
89144
{

IFPainter.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class IFPainter
3535

3636
void drawImage(int x, int y, int width, int height, std::string imgPath);
3737
void drawImageFitted(int x, int y, int width, int height, std::string imgPath);
38+
void drawDiagramFitted(int x, int y, int width, int height, std::string imgPath, std::string text);
3839
void drawText(int x, int y, int height, int width, std::string text, int flags = 0);
3940
void drawTextCentered(int x, int y, int height, int width, std::string text, int flags = 0);
4041
void drawLine(int x1, int y1, int x2, int y2, int width, cv::Scalar color);

PerspectiveGatherer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ std::string renderPerspective(RenderingFace face, Options& opt, std::string comp
9797

9898
std::cout << render << std::endl;
9999

100-
if (std::remove(outputname.c_str()) != 0) {
101-
std::cerr << "Failed to remove " << outputname << std::endl;
102-
}
100+
//if (std::remove(outputname.c_str()) != 0) {
101+
// std::cerr << "Failed to remove " << outputname << std::endl;
102+
//}
103103

104104
try {
105105
auto result2 = system(render.c_str());

RenderHandler.cpp

+23-2
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,36 @@ void makeRenderSection(IFPainter& img, InformationGatherer& info, int offsetX, i
455455
break;
456456
default: // draw face
457457
std::string render = renderPerspective(faceDetails[next].face, opt, info.largestComponents[0].name);
458-
img.drawImageFitted(offsetX + coords[0] + 15, offsetY + coords[1] + 15, coords[2] - coords[0] - 30, coords[3] - coords[1] - 30, render);
458+
459+
//double GAP_PIXELS = 80;
460+
double oldW = coords[2] - coords[0];
461+
double oldH = coords[3] - coords[1];
462+
//double scale = oldW < oldH ? (oldW - GAP_PIXELS) / oldW : (oldH - GAP_PIXELS) / oldH;
463+
double scale = 0.92;
464+
double newW = oldW * scale;
465+
double newH = oldH * scale;
466+
double newX = coords[0] + oldW / 2 - newW / 2;
467+
double newY = coords[1] + oldH / 2 - newH / 2;
468+
469+
img.drawImageFitted(offsetX + newX, offsetY + newY, newW, newH, render);
470+
/*
471+
if (next == '0')
472+
{
473+
img.drawLine(offsetX + newX + GAP_PIXELS / 8, offsetY + newY - GAP_PIXELS / 8, offsetX + newX + newW - GAP_PIXELS/8, offsetY + newY - GAP_PIXELS / 8, 2, cv::Scalar(160, 160, 160));
474+
img.drawLine(offsetX + newX - GAP_PIXELS / 8, offsetY + newY + GAP_PIXELS / 8, offsetX + newX - GAP_PIXELS / 8, offsetY + newY + newH - GAP_PIXELS / 8, 2, cv::Scalar(160, 160, 160));
475+
}
476+
if (next == '1')
477+
{
478+
img.drawLine(offsetX + newX - GAP_PIXELS / 8, offsetY + newY + GAP_PIXELS / 8, offsetX + newX - GAP_PIXELS / 8, offsetY + newY + newH - GAP_PIXELS / 8, 2, cv::Scalar(160, 160, 160));
479+
}*/
459480
break;
460481
}
461482
}
462483

463484
// render ambient occlusion view
464485
std::vector<int> coords = bestLayout.getCoordinates(-1); // fetch ambient occlusion coordinates
465486
std::string render = renderPerspective(DETAILED, opt, info.largestComponents[0].name);
466-
img.drawImageFitted(offsetX + coords[0], offsetY + coords[1], coords[2] - coords[0], coords[3] - coords[1], render);
487+
img.drawDiagramFitted(offsetX + coords[0], offsetY + coords[1], coords[2] - coords[0], coords[3] - coords[1], render, info.getInfo("title"));
467488
}
468489

469490
LayoutChoice selectLayout(int secWidth, int secHeight, double modelLength, double modelDepth, double modelHeight)

main.cpp

+1-17
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,7 @@ void generateReport(Options opt);
66
int main(int argc, char **argv) {
77
Options options;
88
if (readParameters(argc, argv, options)) {
9-
if (options.getIsFolder()) {
10-
int cnt = 1;
11-
for (const auto & entry : std::filesystem::directory_iterator(options.getFolder())) {
12-
options.setFilepath(entry.path());
13-
options.setExportToFile();
14-
std::string filename = options.getFilepath();
15-
filename = filename.substr(filename.find_last_of("/\\") + 1);
16-
filename = filename.substr(0, filename.find_last_of("."));
17-
std::cout << "Processing: " << filename << std::endl;
18-
std::string exportPath = options.getExportFolder() + "/" + filename + "_report.png";
19-
options.setFileName(exportPath);
20-
generateReport(options);
21-
std::cout << "Finished Processing: " << cnt++ << std::endl;
22-
}
23-
} else {
24-
generateReport(options);
25-
}
9+
generateReport(options);
2610
}
2711
}
2812

0 commit comments

Comments
 (0)