Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add my sample, but not ready #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ set(LIBRARY_DEPS ${OpenCV_LIBS})

# BUILD
add_subdirectory(sample_template)
# Add you directory here
# add_subdirectory(sample_YOUR_NAME)
add_subdirectory(sample_ekaterina_maljutina)


# REPORT
message( STATUS "")
Expand Down
7 changes: 7 additions & 0 deletions sample_ekaterina_maljutina/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(target "sample_ekaterina_maljutina")

file(GLOB hdrs "*.hpp")
file(GLOB srcs "*.cpp")

add_executable(${target} ${srcs} ${hdrs})
target_link_libraries(${target} ${LIBRARY_DEPS})
230 changes: 230 additions & 0 deletions sample_ekaterina_maljutina/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
#include "application.hpp"
#include "processing.hpp"
#include "ctime"
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int Application::parseArguments(int argc, const char **argv,
Application::Parameters &params)
{
if (argc < 2)
{
return 1;
}
params.imgFileName = std::string(argv[1]);
return 0;
}

int Application::getFrame(const std::string &fileName, Mat& src)
{
src = imread(fileName);
if (src.empty())
{
return 1;
}
return 0;
}

int Application::processFrame(const Mat& src, Mat& dst)
{


processor.processFrame(src, dst, guiState.filter);

if (dst.empty())
{
return 1;
}

return 0;
}

int Application::drawButtons(Mat &display)
{
guiState.onButtonPlace = Rect(20, display.rows - 60, 120, 40);
guiState.offButtonPlace = Rect(160, display.rows - 60, 120, 40);
guiState.newButtom = Rect(300,display.rows - 60, 120,40);
guiState.FilterMEDIAN = Rect(450,display.rows - 60, 120,40);

guiState.FilterCVT_CONVERT_GRAY = Rect(600,display.rows - 60, 120,40);
guiState.FilterPIXELIZED = Rect(750,display.rows - 60, 120,40);
guiState.FilterCANNY = Rect(900 ,display.rows - 60, 120,40);

rectangle(display, guiState.newButtom,
Scalar(128, 128, 128), CV_FILLED);
rectangle(display, guiState.onButtonPlace,
Scalar(128, 128, 128), CV_FILLED);
rectangle(display, guiState.offButtonPlace,
Scalar(128, 128, 128), CV_FILLED);
rectangle(display, guiState.FilterMEDIAN,
Scalar(128, 128, 128), CV_FILLED);


rectangle(display, guiState.FilterCVT_CONVERT_GRAY,
Scalar(128, 128, 128), CV_FILLED);
rectangle(display, guiState.FilterPIXELIZED,
Scalar(128, 128, 128), CV_FILLED);
rectangle(display, guiState.FilterCANNY,
Scalar(128, 128, 128), CV_FILLED);


putText(display, "on",
Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15,
guiState.onButtonPlace.y + guiState.onButtonPlace.height / 2 + 10),
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);
putText(display, "off",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekaterinaMaljutina поправьте форматирование, у вас проблемы табуляцией.

Point(guiState.offButtonPlace.x + guiState.offButtonPlace.width / 2 - 20,
guiState.offButtonPlace.y + guiState.offButtonPlace.height / 2 + 10),
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);


putText(display, "my_but",
Point(guiState.newButtom.x + guiState.newButtom.width / 2 - 55,
guiState.newButtom.y + guiState.newButtom.height / 2 + 10),
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);


putText(display, "median",
Point(guiState.FilterMEDIAN.x + guiState.FilterMEDIAN.width / 2 -50,
guiState.FilterMEDIAN.y + guiState.FilterMEDIAN.height / 2 + 10),
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);

putText(display, "gray",
Point(guiState.FilterCVT_CONVERT_GRAY.x + guiState.FilterCVT_CONVERT_GRAY.width / 2-45,
guiState.FilterCVT_CONVERT_GRAY.y + guiState.FilterCVT_CONVERT_GRAY.height / 2 + 10),
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);

putText(display, "pixel",
Point(guiState.FilterPIXELIZED.x + guiState.FilterPIXELIZED.width / 2 - 60,
guiState.FilterPIXELIZED.y + guiState.FilterPIXELIZED.height / 2 + 10),
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);

putText(display, "canny",
Point(guiState.FilterPIXELIZED.x + guiState.FilterPIXELIZED.width / 2 + 85,
guiState.FilterPIXELIZED.y + guiState.FilterPIXELIZED.height / 2 + 10),
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);


return 0;
}

int Application::showFrame(const std::string &caption,
const Mat& src, Mat& dst)
{

if (guiState.state == OffFilter)
{
src.copyTo(dst);
}
else if (guiState.state == OnFilter)
{
processFrame(src, dst);
}
else
{
return 1;
}

Mat display(src.rows, src.cols + dst.cols, src.type());
Mat srcRoi = display(Rect(0, 0, src.cols, src.rows));
src.copyTo(srcRoi);
Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows));
dst.copyTo(dstRoi);

if (guiState.save)
{

// �������� ������� �����
time_t rawtime;
struct tm * timeinfo;

time (&rawtime);
timeinfo = localtime(&rawtime);


// ������������� �������� �����������
std::ostringstream oss;
oss << rawtime;
string str = oss.str();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekaterinaMaljutina генерацию названия файла для сохранения стоит вынести в скрытый метод.

string name_img = "image_sample_ekaterina_maljutina_" ;

// <image_name> - ��������������� �������� �����������
// � ������ �������� �������

imwrite(name_img +str + ".jpg" , display);

guiState.save = false;
// ������� ������� ���������� imwrite(<image_name>, display)
// �������� �������� guiState.saveState � false
}

drawButtons(display);

namedWindow(caption);
imshow(caption, display);
setMouseCallback(caption, onButtonsOnOffClick, &guiState);
char key = waitKey(1);

return key;
}

void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData)
{
if (eventId != EVENT_LBUTTONDOWN)
{
return;
}
Application::GUIElementsState *elems =
(Application::GUIElementsState *)userData;
if (onButtonClicked(elems->onButtonPlace, x, y))
{
elems->state = Application::OnFilter;
return;
}
if (onButtonClicked(elems->offButtonPlace, x, y))
{
elems->state = Application::OffFilter;
return;
}
if (onButtonClicked(elems->newButtom, x, y))
{
elems->save = true;
return;
}
if (onButtonClicked(elems->FilterMEDIAN, x, y))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

опять с форматированием проблемы

{
elems->state = Application::OnFilter;
elems->filter = Processing::MEDIAN;
return;
}
if (onButtonClicked(elems->FilterCVT_CONVERT_GRAY, x, y))
{
elems->state = Application::OnFilter;
elems->filter = Processing::CVT_CONVERT_GRAY;
return;
}
if (onButtonClicked(elems->FilterPIXELIZED, x, y))
{
elems->state = Application::OnFilter;
elems->filter = Processing::PIXELIZED;
return;
}
if (onButtonClicked(elems->FilterCANNY, x, y))
{
elems->state = Application::OnFilter;
elems->filter = Processing::CANNY;
return;
}
}

bool onButtonClicked(cv::Rect buttonPlace, int x, int y)
{
if (x < buttonPlace.x || x > buttonPlace.x + buttonPlace.width ||
y < buttonPlace.y || y > buttonPlace.y + buttonPlace.height)
{
return false;
}
return true;
}

63 changes: 63 additions & 0 deletions sample_ekaterina_maljutina/application.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <string>
#include <iostream>
#include <opencv2/core/core.hpp>

#include "processing.hpp"

bool onButtonClicked(cv::Rect buttonPlace, int x, int y);
void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData);

class Application
{
public:
enum WindowState
{
OnFilter,
OffFilter
};
struct Parameters
{
std::string imgFileName;
};
struct GUIElementsState
{
WindowState state;
cv::Rect onButtonPlace;
cv::Rect offButtonPlace;
cv::Rect newButtom;

cv::Rect FilterMEDIAN;
cv::Rect FilterCVT_CONVERT_GRAY;
cv::Rect FilterPIXELIZED;
cv::Rect FilterCANNY;

Processing::FilterType filter;

bool save;
};
int parseArguments(int argc, const char **argv, Parameters &params);
int getFrame(const std::string &fileName, cv::Mat& src);
int processFrame(const cv::Mat& src, cv::Mat& dst);
int showFrame(const std::string &caption,
const cv::Mat& src, cv::Mat& dst);
friend void onButtonsOnOffClick(int eventId, int x, int y,
int flags, void *userData);
Application()
{
guiState.state = OnFilter;
guiState.save = false;

guiState.filter = Processing::MEDIAN;

};

private:
Processing processor;
GUIElementsState guiState;

int drawButtons(cv::Mat &display);

friend bool onButtonClicked(cv::Rect buttonPlace, int x, int y);
};
44 changes: 44 additions & 0 deletions sample_ekaterina_maljutina/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <opencv2/core/core.hpp>
#include <iostream>

#include "application.hpp"

using namespace std;
using namespace cv;

enum ErrorCode {
OK,
WRONG_ARGUMENTS,
WRONG_INPUT,
CANT_PROCESS
};

int main(int argc, const char **argv)
{
Application app;
Application::Parameters params;

if (app.parseArguments(argc, argv, params) != 0)
{
cout << "sample_template <image_name>" << endl;
cout << "<image_name> - image name for filtering" << endl;
return WRONG_ARGUMENTS;
}

Mat src;
if (app.getFrame(params.imgFileName, src) != 0)
{
cout << "Error: \'src\' image is null or empty!" << endl;
return WRONG_INPUT;
}

const std::string caption = "OpenCV Sample";
char key = 0;
Mat dst(src.rows, src.cols, src.type());
while (key != 27) // Esc
{
key = app.showFrame(caption, src, dst);
}

return OK;
}
Loading