Skip to content

Commit

Permalink
fix/format: fixing some bugs in active contours / structure reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamel-Mohammed committed Mar 24, 2023
1 parent d2beeb3 commit 0c78a87
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 115 deletions.
50 changes: 26 additions & 24 deletions CV/snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,28 @@ void snake::snake_operation(Mat image, vector<Point>& curve, int window_size, do
int windowIndex = (window_size - 1) / 2;
int numPoints = curve.size();
vector<Point> newCurve(numPoints);
for (int i = 0; i < numPoints; i++) {
Point pt = curve[i];
Point prevPt = curve[(i-1+numPoints)%numPoints];
Point nextPt = curve[(i+1)%numPoints];
double minEnergy = DBL_MAX; // max value can a double variable hold
Point newPt = pt;
// Try moving the point in different directions and choose the one with the minimum energy
for (int dx = -windowIndex; dx <= windowIndex; dx++) {
for (int dy = -windowIndex; dy <= windowIndex; dy++) {
Point movePt(pt.x + dx, pt.y + dy);
double energy = calculate_point_enegy(image, movePt, prevPt, nextPt, alpha, beta, gamma);
if (energy < minEnergy) {
minEnergy = energy;
newPt = movePt;

for (int i = 0; i < numPoints; i++) {
Point pt = curve[i];
Point prevPt = curve[(i-1+numPoints)%numPoints];
Point nextPt = curve[(i+1)%numPoints];
double minEnergy = DBL_MAX; // max value can a double variable hold
Point newPt = pt;
// Try moving the point in different directions and choose the one with the minimum energy
for (int dx = -windowIndex; dx <= windowIndex; dx++) {
for (int dy = -windowIndex; dy <= windowIndex; dy++) {
Point movePt(pt.x + dx, pt.y + dy);
double energy = calculate_point_enegy(image, movePt, prevPt, nextPt, alpha, beta, gamma);
if (energy < minEnergy) {
minEnergy = energy;
newPt = movePt;
}
}
}
newCurve[i] = newPt;
}
newCurve[i] = newPt;
}


curve = newCurve;
}

Expand Down Expand Up @@ -147,7 +150,11 @@ void snake::draw_contours(Mat image, Mat &outputimage, vector<Point> snake_point
vector<Point> snake::active_contour(Mat inputimage, Mat &outputimage,
Point center, int radius,
int numOfIterations, int numOfPoints,
int window_size, double alpha, double beta, double gamma, QLabel*label){
int window_size, double alpha, double beta, double gamma){

cout << center << endl;
cout << (inputimage.cols / 2) << " " << (inputimage.rows / 2) << endl;


vector<Point> curve = initialize_contours(center, radius, numOfPoints);

Expand All @@ -159,14 +166,9 @@ vector<Point> snake::active_contour(Mat inputimage, Mat &outputimage,
// Iterate for multiple iterations
for (int i = 0; i < numOfIterations; i++) {
snake_operation(grayimage, curve, window_size, alpha, beta, gamma);
draw_contours(inputimage, outputimage, curve);
QImage qimage(outputimage.data, outputimage.cols, outputimage.rows,outputimage.step,QImage::Format_BGR888);
QPixmap* image = new QPixmap(QPixmap::fromImage(qimage));
int w = label->width();
int h = label->height();
label->setPixmap(image->scaled(w,h,Qt::KeepAspectRatio));
}

// draw_contours(inputimage, outputimage, curve);
draw_contours(inputimage, outputimage, curve);

return curve;
}
2 changes: 1 addition & 1 deletion CV/snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class snake
static vector<Point> active_contour(Mat inputimage, Mat &outputimage,
Point center, int radius,
int numOfIterations, int numOfPoints,
int window_size, double alpha, double beta, double gamma,QLabel * label);
int window_size, double alpha, double beta, double gamma);

static double calculate_contour_area(vector<Point> snake_points);
static double calculate_contour_perimeter(vector<Point> snake_points);
Expand Down
3 changes: 1 addition & 2 deletions Plot/plotter.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef PLOTTER_H
#define PLOTTER_H
#include "qcustomplot.h"
#include <QVector>

#include "utilities/qcustomplot.h"
class plotter
{
public:
Expand Down
42 changes: 21 additions & 21 deletions a01-team06.pro
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ SOURCES += \
CV/histogram.cpp \
CV/filters.cpp \
CV/frequency.cpp \
# Plot/plotter.cpp \
Plot/plotter.cpp \
CV/hough.cpp \
CV/snake.cpp \
# pages/page4.cpp \
# qcustomplot.cpp \
clickable.cpp \
scene.cpp \
pages/page4.cpp \
utilities/qcustomplot.cpp \
utilities/clickable.cpp \
utilities/scene.cpp \
main.cpp \
pages/page3.cpp \
pages/mainwindow.cpp \
# pages/page2.cpp \
pages/page2.cpp \
pages/page1.cpp \
pages/page5.cpp \
pages/page6.cpp
Expand All @@ -33,37 +33,37 @@ HEADERS += \
CV/histogram.h \
CV/filters.h \
CV/frequency.h \
# Plot/plotter.h \
Plot/plotter.h \
CV/hough.h \
CV/snake.h \
# pages/page4.h \
# qcustomplot.h \
clickable.h \
pages/page4.h \
utilities/qcustomplot.h \
utilities/clickable.h \
pages/page3.h \
pages/mainwindow.h \
# pages/page2.h \
pages/page2.h \
pages/page1.h \
pages/page5.h \
pages/page6.h \
scene.h
utilities/scene.h

FORMS += \
# pages/page4.ui \
pages/page4.ui \
pages/page3.ui \
pages/mainwindow.ui \
# pages/page2.ui \
pages/page2.ui \
pages/page1.ui \
pages/page5.ui \
pages/page6.ui

INCLUDEPATH += C:\opencv\build\include
INCLUDEPATH += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\build\include

LIBS += C:\opencv\release\bin\libopencv_core470.dll
LIBS += C:\opencv\release\bin\libopencv_highgui470.dll
LIBS += C:\opencv\release\bin\libopencv_imgcodecs470.dll
LIBS += C:\opencv\release\bin\libopencv_imgproc470.dll
LIBS += C:\opencv\release\bin\libopencv_features2d470.dll
LIBS += C:\opencv\release\bin\libopencv_calib3d470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_core470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_highgui470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_imgcodecs470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_imgproc470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_features2d470.dll
LIBS += C:\Users\kamel\OneDrive\Documents\libraries\opencv\opencv\release\bin\libopencv_calib3d470.dll

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
Expand Down
Binary file added images/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Screenshot 2023-02-28 231508.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/ellipses.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 2 additions & 26 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,18 @@
#include <QPixmap>
#include <QCursor>
#include <QPoint>
#include "clickable.h"
#include "utilities/clickable.h"

void changeLabelCursors(const QCursor &newCursor)
{
// Iterate through all widgets in the application
foreach (QWidget *widget, QApplication::allWidgets())
{
qDebug()<<"Here";
// Check if the widget is a QLabel
QLabel *label = qobject_cast<QLabel*>(widget);
if (label != nullptr)
{
// Set the new cursor for the QLabel
label->setCursor(newCursor);
}
}
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// QPixmap pixmap(":/resources/resources/handpointing.png");
// QPoint hotSpot(11, 10);
// QCursor cursor(pixmap, hotSpot.x(), hotSpot.y());
// changeLabelCursors(cursor);
// QApplication::setOverrideCursor(cursor);

MainWindow w;

w.show();
a.exec();

QList<QWidget*> widgets = a.allWidgets();
qDebug()<<widgets.size();

return 0;
return a.exec();
}


33 changes: 11 additions & 22 deletions pages/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ MainWindow::MainWindow(QWidget *parent)
{
ui->setupUi(this);

QString fileName = "Cstartpage.qss";
QString projectDirPath =__FILE__;
projectDirPath+="/../../style";
QString filePath = QDir(projectDirPath).absoluteFilePath(fileName);
QFile f(filePath);
QFile f(":/resources/style/Cstartpage.qss");
if ( !f.exists() )
{
qWarning() << "Unable to set dark stylesheet, file not found";
Expand All @@ -28,36 +24,29 @@ MainWindow::MainWindow(QWidget *parent)
QPoint hotSpot(11, 10);
QCursor cursor(pixmap, hotSpot.x(), hotSpot.y());
this->setCursor(cursor);


QButtonGroup *group = new QButtonGroup();
group->addButton(ui->pushButton);
group->addButton(ui->pushButton2);
group->addButton(ui->pushButton_3);
group->addButton(ui->pushButton_4);

this->setWindowTitle("CV ToolKit");

ui->stackedWidget->insertWidget(0,new page1());
// ui->stackedWidget->insertWidget(1,new page2());
ui->stackedWidget->insertWidget(1,new page2());
ui->stackedWidget->insertWidget(2,new page3());
//ui->stackedWidget->insertWidget(3,new page4());
ui->stackedWidget->insertWidget(4,new page5());
ui->stackedWidget->insertWidget(3,new page4());
ui->stackedWidget->insertWidget(4,new page5());
ui->stackedWidget->insertWidget(5,new Page6());


ui->stackedWidget->setCurrentIndex(0);
connect(ui->pushButton, &QPushButton::clicked, ui->stackedWidget, [=]() {
ui->stackedWidget->setCurrentIndex(0);
});
// connect(ui->pushButton2, &QPushButton::clicked, ui->stackedWidget, [=]() {
// ui->stackedWidget->setCurrentIndex(1);
// });
connect(ui->pushButton2, &QPushButton::clicked, ui->stackedWidget, [=]() {
ui->stackedWidget->setCurrentIndex(1);
});
connect(ui->pushButton_3, &QPushButton::clicked, ui->stackedWidget, [=]() {
ui->stackedWidget->setCurrentIndex(2);
});
// connect(ui->pushButton_4, &QPushButton::clicked, ui->stackedWidget, [=]() {
// ui->stackedWidget->setCurrentIndex(3);
// });
connect(ui->pushButton_4, &QPushButton::clicked, ui->stackedWidget, [=]() {
ui->stackedWidget->setCurrentIndex(3);
});
connect(ui->pushButton_5, &QPushButton::clicked, ui->stackedWidget, [=]() {
ui->stackedWidget->setCurrentIndex(4);
});
Expand Down
2 changes: 1 addition & 1 deletion pages/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include"pages/page6.h"
#include "pages/page4.h"
#include"page5.h"
#include "clickable.h"
#include "utilities/clickable.h"
#include <QFile>
#include <QDir>
#include <QString>
Expand Down
19 changes: 8 additions & 11 deletions pages/page5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void page5::on_window_size_valueChanged(int arg1)

void page5::on_upload_btn_clicked()
{
Img= QFileDialog::getOpenFileName(this, "Open Image", "", "Image Files (*.png *.jpg *.bmp)");
Img= QFileDialog::getOpenFileName(this, "Open Image", "", "Image Files (*.png *.jpg *.bmp *.jpeg)");
QImage image(Img);
scene = new Scene();
scene->setRad(ui->raduis->value());
Expand All @@ -46,7 +46,6 @@ void page5::updateImage(){
ui->graphicsView->setScene(scene);
ui->graphicsView->fitInView(scene->sceneRect(),Qt::KeepAspectRatio);
}

}


Expand All @@ -73,12 +72,13 @@ void page5::resizeEvent(QResizeEvent *e)

void page5::on_generate_btn_clicked()
{

Mat imageCv= cv::imread(Img.toStdString(),IMREAD_COLOR);
Mat output;
Point center= Point_<double>(scene->getCenter().x(), scene->getCenter().y());

vector<Point> s_vector =snake::active_contour(imageCv,output,center,ui->raduis->value(),ui->numItr->value(),ui->num_points->value(),
ui->window_size->value(),ui->alpha->value(),ui->beta->value(),ui->gamma->value(),ui->output_label);
ui->window_size->value(),ui->alpha->value(),ui->beta->value(),ui->gamma->value());

double area = snake::calculate_contour_area(s_vector);
double perimeter=snake::calculate_contour_perimeter(s_vector);
Expand All @@ -87,13 +87,10 @@ void page5::on_generate_btn_clicked()
ui->area->setValue(area);
ui->perimeter->setValue(perimeter);

// QImage qimage(output.data, output.cols, output.rows,output.step,QImage::Format_BGR888);
// QPixmap* image = new QPixmap(QPixmap::fromImage(qimage));
// int w = ui->output_label->width();
// int h = ui->output_label->height();
// ui->output_label->setPixmap(image->scaled(w,h,Qt::KeepAspectRatio));



QImage qimage(output.data, output.cols, output.rows,output.step,QImage::Format_BGR888);
QPixmap* image = new QPixmap(QPixmap::fromImage(qimage));
int w = ui->output_label->width();
int h = ui->output_label->height();
ui->output_label->setPixmap(image->scaled(w,h,Qt::KeepAspectRatio));
}

2 changes: 1 addition & 1 deletion pages/page5.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define PAGE5_H

#include <QWidget>
#include "Scene.h"
#include "utilities/scene.h"
#include <QGraphicsScene>
#include <QFileDialog>
#include "CV/snake.h"
Expand Down
Loading

0 comments on commit 0c78a87

Please sign in to comment.