Skip to content

Commit

Permalink
adding page4 with HoughTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
youssef-shaban committed Mar 20, 2023
1 parent 9f6df7c commit bfe11e4
Show file tree
Hide file tree
Showing 19 changed files with 731 additions and 52 deletions.
120 changes: 120 additions & 0 deletions CV/hough.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "hough.h"


void Hough::HoughLine(cv::Mat edges, std::vector<cv::Vec2d> &lines, int threshold, double angleRes)
{
int width = edges.cols;
int height = edges.rows;
double rhoMax = sqrt(width * width + height * height);
int rhoSize = 2 * round(rhoMax);
int thetaSize = 180 * angleRes;
cv::Mat accumulator = cv::Mat::zeros(rhoSize, thetaSize, CV_32SC1);

std::vector<double> thetas(thetaSize);
for (int i = 0; i < thetaSize; i++)
{
thetas[i] = i / angleRes;
}
std::cout << thetas[thetas.size() - 1];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (edges.at<uchar>(y, x) > 0)
{
for (int i = 0; i < thetaSize; i++)
{
double rho = x * cos(thetas[i]) + y * sin(thetas[i]);
int rhoIndex = round(rho + rhoMax);
accumulator.at<int>(rhoIndex, i)++;
}
}
}
}
for (int y = 0; y < rhoSize; y++)
{

for (int x = 0; x < thetaSize; x++)
{
if (accumulator.at<int>(y, x) >= threshold)
{
double rho = y - rhoMax;
double theta = x / angleRes;
lines.push_back(cv::Vec2d(rho, theta));
}
}
}
}

void Hough::drawLines(cv::Mat &img, std::vector<cv::Vec2d> lines, cv::Scalar color)
{
for (size_t i = 0; i < lines.size(); i++)
{
double rho = lines[i][0];
double theta = lines[i][1];
double a = cos(theta);
double b = sin(theta);
double x0 = a * rho;
double y0 = b * rho;
cv::Point pt1(cvRound(x0 + 1000 * (-b)), cvRound(y0 + 1000 * a));
cv::Point pt2(cvRound(x0 - 1000 * (-b)), cvRound(y0 - 1000 * a));
cv::line(img, pt1, pt2,color, 1);
}
}

void Hough::HoughCircle(cv::Mat edges, std::vector<cv::Vec3d> &circles, int threshold, int minRadius, int maxRadius, int radiusStep, QProgressBar* bar)
{
std::vector<std::vector<std::vector<int>>> accumulator(edges.rows, std::vector<std::vector<int>>(edges.cols, std::vector<int>(maxRadius / radiusStep, 0)));
for (int r = minRadius; r < maxRadius; r += radiusStep)
{
bar->setValue((r -(double)minRadius)/ (maxRadius - (double)minRadius) * 100);
for (int y = 0; y < edges.rows; y++)
{
for (int x = 0; x < edges.cols; x++)
{
if (edges.at<uchar>(y, x) == 255)
{
for (int k = 0; k < 180; k++)
{
int a = x - r * std::cos(k);
int b = y - r * std::sin(k);

if (a >= 0 && a < edges.cols && b >= 0 && b < edges.rows)
{
accumulator[b][a][r / radiusStep]++;
}
}
}
}
}
}

for (int y = 0; y < edges.rows; y++)
{
for (int x = 0; x < edges.cols; x++)
{
for (int r = 0; r < maxRadius / radiusStep; r++)
{
if (accumulator[y][x][r] > threshold)
{
circles.push_back(cv::Vec3d(x, y, r * radiusStep));
}
}
}
}
bar->setValue(100);
}

void Hough::drawCircles(cv::Mat &img, std::vector<cv::Vec3d> circles, cv::Scalar color)
{
for (int i = 0; i < circles.size(); i++)
{
cv::Point center = cv::Point(circles[i][0], circles[i][1]);
cv::Scalar color(0, 255, 0);
int size = 5;
cv::circle(img, center, circles[i][2], cv::Scalar(0, 0, 255), 2);
cv::line(img, cv::Point(center.x - size, center.y), cv::Point(center.x + size, center.y), color, 1);
cv::line(img, cv::Point(center.x, center.y - size), cv::Point(center.x, center.y + size), color, 1);

}
}
19 changes: 19 additions & 0 deletions CV/hough.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef HOUGH_H
#define HOUGH_H
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <QProgressBar>

class Hough
{
public:
Hough();
static void HoughLine(cv::Mat edges, std::vector<cv::Vec2d>& lines, int threshold, double angleRes);
static void drawLines(cv::Mat& img, std::vector<cv::Vec2d> lines, cv::Scalar color);
static void HoughCircle(cv::Mat edges, std::vector<cv::Vec3d>& circles,int threshold, int minRadius, int maxRadius, int radiusStep,QProgressBar* bar);
static void drawCircles(cv::Mat& img, std::vector<cv::Vec3d> circles, cv::Scalar color);
};

#endif // HOUGH_H
35 changes: 19 additions & 16 deletions a01-team06.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,45 @@ SOURCES += \
CV/histogram.cpp \
CV/filters.cpp \
CV/frequency.cpp \
Plot/plotter.cpp \
qcustomplot.cpp \
# Plot/plotter.cpp \
CV/hough.cpp \
pages/page4.cpp \
# qcustomplot.cpp \
clickable.cpp \
main.cpp \
pages/page3.cpp \
pages/mainwindow.cpp \
pages/page2.cpp \
# pages/page2.cpp \
pages/page1.cpp
HEADERS += \
CV/histogram.h \
CV/filters.h \
CV/frequency.h \
Plot/plotter.h \
qcustomplot.h \
# Plot/plotter.h \
CV/hough.h \
pages/page4.h \
# qcustomplot.h \
clickable.h \
pages/page3.h \
pages/mainwindow.h \
pages/page2.h \
# pages/page2.h \
pages/page1.h

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

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

INCLUDEPATH += C:\opencv\release\install\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_calib3d470.dll
LIBS += C:\opencv\release\bin\libopencv_features2d470.dll

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

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
Expand Down
35 changes: 34 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
#include "pages/mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QCursor>
#include <QPoint>
#include "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();
return a.exec();
a.exec();

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

return 0;
}


35 changes: 30 additions & 5 deletions pages/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ MainWindow::MainWindow(QWidget *parent)
QString projectDirPath =__FILE__;
projectDirPath+="/../../style";
QString filePath = QDir(projectDirPath).absoluteFilePath(fileName);
qDebug()<<filePath;
QFile f(filePath);
if ( !f.exists() )
{
Expand All @@ -29,27 +28,53 @@ MainWindow::MainWindow(QWidget *parent)
QTextStream ts( &f );
this->setStyleSheet( ts.readAll() );
}
QPixmap pixmap(":/resources/resources/default.png");
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);


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->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);
});


QPixmap pointPix(":/resources/resources/handpointing.png");
QPoint hotSpot2(11, 10);
QCursor pointerCursor(pointPix, hotSpot2.x(), hotSpot2.y());
QList<QWidget *> widgets= this->findChildren<QWidget *>();
foreach (QWidget *widget, widgets)
{
// Check if the widget is a QLabel
Clickable *label = qobject_cast<Clickable*>(widget);
if (label != nullptr)
{
// Set the new cursor for the QLabel
label->setCursor(pointerCursor);
continue;
}
}

}

Expand Down
2 changes: 2 additions & 0 deletions pages/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "page3.h"
#include "page2.h"
#include "page1.h"
#include "pages/page4.h"
#include "clickable.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
Expand Down
7 changes: 7 additions & 0 deletions pages/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>Page 4</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand Down
4 changes: 4 additions & 0 deletions pages/page1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ page1::page1(QWidget *parent) :
ui->setupUi(this);
ui->stackedWidget->setCurrentIndex(0);
ui->label1_1->setText("Noise Value");
QPixmap pixmap(":/resources/resources/handpointing.png");
QPoint hotSpot(11, 10);
QCursor cursor(pixmap, hotSpot.x(), hotSpot.y());
ui->image->setCursor(cursor);
}

page1::~page1()
Expand Down
16 changes: 2 additions & 14 deletions pages/page1.ui
Original file line number Diff line number Diff line change
Expand Up @@ -461,23 +461,13 @@
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="Clickable" name="image">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">QLabel:hover {
background-color: #33b78620;
border: 1px solid #d66d31;

}
<string notr="true">
</string>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="../resources.qrc">:/resources/resources/icons8-upload-32.png</pixmap>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
Expand Down Expand Up @@ -526,8 +516,6 @@ background-color: #33b78620;
</slots>
</customwidget>
</customwidgets>
<resources>
<include location="../resources.qrc"/>
</resources>
<resources/>
<connections/>
</ui>
6 changes: 1 addition & 5 deletions pages/page3.ui
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@
<string/>
</property>
<property name="styleSheet">
<string notr="true">QLabel:hover {
background-color: #33b78620;
border: 1px solid #d66d31;

}</string>
<string notr="true"/>
</property>
<property name="text">
<string/>
Expand Down
Loading

0 comments on commit bfe11e4

Please sign in to comment.