Skip to content

Commit 5e77ab9

Browse files
committed
nothing to say
1 parent 39520b9 commit 5e77ab9

File tree

5 files changed

+273
-7
lines changed

5 files changed

+273
-7
lines changed

main.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "mainwindow.h"
2+
#include "mywindow.h"
23
#include <QApplication>
34
// json library from https://github.com/danielaparker/jsoncons
45
#include "jsoncons/json.hpp"
@@ -151,6 +152,10 @@ void tictoc(string msg="") {
151152

152153
int main(int argc, char *argv[])
153154
{
155+
/*
156+
* http://stackoverflow.com/questions/17979185/qt-5-1-qapplication-without-display-qxcbconnection-could-not-connect-to-displ
157+
* if not going to use gui, add -platform offscreen command line option
158+
* */
154159
QApplication a(argc, argv);
155160
if (argc >= 2 && string(argv[1])=="server") {
156161
ImageCutController ctl;
@@ -204,6 +209,7 @@ int main(int argc, char *argv[])
204209
res["status"] = "error";
205210
res["error"] = string("command '")+cmdstr+("' not found");
206211
}
212+
//cerr << cmdstr << ' ' << res.to_string() << endl;
207213
} catch (...) {
208214
res["status"] = "error";
209215
res["error"] = "fatal error occur";
@@ -214,7 +220,7 @@ int main(int argc, char *argv[])
214220
tictoc("calc cmd");
215221
}
216222
} else {
217-
MainWindow w;
223+
MyWindow w;
218224
w.show();
219225

220226
return a.exec();

mywindow.cpp

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#include "mywindow.h"
2+
#include "ui_mywindow.h"
3+
#include <QFileDialog>
4+
5+
template<class T> T sqr(T a) {return a*a;}
6+
7+
MyWindow::MyWindow(QWidget *parent) :
8+
QMainWindow(parent),
9+
ui(new Ui::MyWindow)
10+
{
11+
ui->setupUi(this);
12+
ui->menubar->setNativeMenuBar(false);
13+
draw_mask.resize(1000,1000,0xff000000);
14+
for (int i=0; i<draw_mask.w; i++)
15+
for (int j=0; j<draw_mask.h; j++)
16+
draw_mask.get(i,j) = 0xff000000 | (j&255);
17+
//on_actionOpen_file_triggered();
18+
}
19+
20+
void drawCircle(MyImage &image, int x, int y, int r, int c) {
21+
int lx = x, rx = x;
22+
int ly = y-r, ry = y+r;
23+
ry = min(ry,image.h-1);
24+
ly = max(ly, 0);
25+
for (int yy = ly; yy<=ry; yy++) {
26+
if (yy<=y)
27+
while (sqr(lx-x)+sqr(yy-y) <= r*r) lx++;
28+
else
29+
while (sqr(lx-x-1)+sqr(yy-y) > r*r) lx--;
30+
rx = x+x-lx;
31+
int llx = min(lx,image.w);
32+
int rrx = max(rx+1, 0);
33+
for (int xx=rrx; xx<llx; xx++)
34+
image.get(xx,yy) = c;
35+
}
36+
}
37+
38+
void drawLine(MyImage &image, int x1, int y1, int x2, int y2, int w, int c) {
39+
if (x1==x2 && y1==y2) return;
40+
double dx = x2-x1, dy = y2-y1;
41+
double norm = sqrt(dx*dx+dy*dy);
42+
dx /= norm, dy /= norm;
43+
double of1 = x1*dx + y1*dy;
44+
double of2 = x2*dx + y2*dy;
45+
double dx2 = dy, dy2 = -dx;
46+
47+
double of3 = x1*dx2 + y1*dy2 - w;
48+
double of4 = x1*dx2 + y1*dy2 + w;
49+
50+
double tmp = abs(dy2 * w);
51+
double py1, py2;
52+
if (y1>y2) {
53+
py1 = y1+tmp;
54+
py2 = y2-tmp;
55+
} else {
56+
py1 = y2+tmp;
57+
py2 = y1-tmp;
58+
}
59+
py2 = max(py2, 0.0);
60+
py1 = min(py1, image.h*1.0);
61+
for (int yy = floor(py2); yy<py1; yy++) {
62+
double xa,xb,xc,xd;
63+
if (dx == 0) {
64+
xa = -1e30;
65+
xb = 1e30;
66+
} else {
67+
xa = (of1 - yy*dy)/dx;
68+
xb = (of2 - yy*dy)/dx;
69+
if (xa>xb) swap(xa,xb);
70+
}
71+
72+
if (dx2 == 0) {
73+
xc = -1e30;
74+
xd = 1e30;
75+
} else {
76+
xc = (of3 - yy*dy2)/dx2;
77+
xd = (of4 - yy*dy2)/dx2;
78+
if (xc>xd) swap(xc,xd);
79+
}
80+
xa = max(xa,xc);
81+
xb = min(xb,xd);
82+
xa = max(xa, 0.0);
83+
xb = min(xb, image.w*1.0);
84+
85+
for (int xx=ceil(xa); xx<xb; xx++)
86+
image.get(xx,yy) = c;
87+
}
88+
}
89+
90+
MyWindow::~MyWindow()
91+
{
92+
delete ui;
93+
}
94+
95+
96+
void MyWindow::paintEvent(QPaintEvent *e) {
97+
QPainter painter(this);
98+
99+
QImage image((uchar *)&draw_mask.get(), draw_mask.w, draw_mask.h, QImage::Format_RGB32);
100+
painter.drawImage(0,0,image);
101+
/*
102+
QPainterPathStroker st;
103+
st.setWidth(10);
104+
st.setJoinStyle(Qt::RoundJoin);
105+
st.setCapStyle(Qt::RoundCap);
106+
107+
QPainterPath spath = st.createStroke(fg_path);
108+
QPainterPath spath2 = st.createStroke(bg_path);
109+
110+
painter.setPen(Qt::NoPen);
111+
painter.setBrush(QColor(0,255,0,100));
112+
painter.drawPath(spath);
113+
painter.setBrush(QColor(255,0,0,100));
114+
painter.drawPath(spath2);
115+
*/
116+
}
117+
118+
void MyWindow::mouseMoveEvent(QMouseEvent *e) {
119+
//qDebug() << e->pos() << endl;
120+
current_path->lineTo(e->pos());
121+
drawCircle(draw_mask, e->pos().x(), e->pos().y(), 5, 0xffcc0000);
122+
drawLine(draw_mask, prev_pos.x(), prev_pos.y(), e->pos().x(), e->pos().y(), 5, 0xffcc0000);
123+
prev_pos = e->pos();
124+
update();
125+
}
126+
127+
void MyWindow::mousePressEvent(QMouseEvent *e) {
128+
prev_pos = e->pos();
129+
drawCircle(draw_mask, e->pos().x(), e->pos().y(), 5, 0xffcc0000);
130+
if (e->button()==Qt::LeftButton) {
131+
current_path = &fg_path;
132+
} else {
133+
current_path = &bg_path;
134+
}
135+
//mousepath = QPainterPath();
136+
current_path->moveTo(e->pos());
137+
update();
138+
}
139+
140+
void MyWindow::mouseReleaseEvent(QMouseEvent *) {
141+
142+
}
143+
144+
void MyWindow::on_actionOpen_file_triggered()
145+
{
146+
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
147+
"/home",
148+
tr("Images (*.png *.xpm *.jpg)"));
149+
QImage img(fileName);
150+
img = img.convertToFormat(QImage::Format_RGB32);
151+
draw_mask.resize(img.width(), img.height());
152+
memcpy(&draw_mask.buffer[0], img.bits(), 4*img.width()*img.height());
153+
this->resize(img.width(), img.height());
154+
update();
155+
}

mywindow.h

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef MYWINDOW_H
2+
#define MYWINDOW_H
3+
4+
#include <QMainWindow>
5+
#include <QPaintEvent>
6+
#include <QPainterPath>
7+
#include <QPainter>
8+
9+
namespace Ui {
10+
class MyWindow;
11+
}
12+
13+
#include <vector>
14+
15+
using namespace std;
16+
class MyImage {
17+
public:
18+
int w,h;
19+
vector<unsigned int> buffer;
20+
MyImage(int _w=0, int _h=0) : w(_w), h(_h) {
21+
buffer.resize(w*h, 0);
22+
}
23+
24+
void resize(int _w, int _h, unsigned int fill=0) {
25+
w = _w, h = _h;
26+
buffer.resize(w*h, fill);
27+
}
28+
unsigned int &get(int i=0, int j=0) {return buffer[i+j*w];}
29+
};
30+
31+
class MyWindow : public QMainWindow
32+
{
33+
Q_OBJECT
34+
35+
public:
36+
explicit MyWindow(QWidget *parent = 0);
37+
~MyWindow();
38+
39+
void paintEvent(QPaintEvent *);
40+
void mouseMoveEvent(QMouseEvent *);
41+
void mousePressEvent(QMouseEvent *);
42+
void mouseReleaseEvent(QMouseEvent *);
43+
44+
45+
QPainterPath fg_path,bg_path, *current_path;
46+
MyImage draw_mask;
47+
QPoint prev_pos;
48+
49+
50+
private slots:
51+
void on_actionOpen_file_triggered();
52+
53+
private:
54+
Ui::MyWindow *ui;
55+
};
56+
57+
#endif // MYWINDOW_H

mywindow.ui

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MyWindow</class>
4+
<widget class="QMainWindow" name="MyWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>800</width>
10+
<height>600</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralwidget"/>
17+
<widget class="QMenuBar" name="menubar">
18+
<property name="geometry">
19+
<rect>
20+
<x>0</x>
21+
<y>0</y>
22+
<width>800</width>
23+
<height>25</height>
24+
</rect>
25+
</property>
26+
<widget class="QMenu" name="menuFile">
27+
<property name="title">
28+
<string>file</string>
29+
</property>
30+
<addaction name="actionOpen_file"/>
31+
</widget>
32+
<addaction name="menuFile"/>
33+
</widget>
34+
<widget class="QStatusBar" name="statusbar"/>
35+
<action name="actionOpen_file">
36+
<property name="checkable">
37+
<bool>true</bool>
38+
</property>
39+
<property name="text">
40+
<string>open file</string>
41+
</property>
42+
</action>
43+
</widget>
44+
<resources/>
45+
<connections/>
46+
</ui>

sumsang.pro

+8-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ SOURCES += main.cpp\
3535
./zz/meanshiftSegmentation/RAList.cpp \
3636
./zz/meanshiftSegmentation/rlist.cpp \
3737
cutoutsettings.cpp \
38-
alphamatting.cpp
38+
alphamatting.cpp \
39+
mywindow.cpp
3940

4041
HEADERS += mainwindow.h \
4142
./zz/GMM/CmCv.h \
@@ -46,14 +47,12 @@ HEADERS += mainwindow.h \
4647
./zz/GMM/CmSetting.h \
4748
./zz/GMM/CmShow.h \
4849
./zz/GMM/CmUFSet.h \
49-
./zz/GMM/StdAfx.h \
5050
./zz/graphCut/adjacency_list/block.h \
5151
./zz/graphCut/adjacency_list/graph.h \
5252
./zz/graphCut/block.h \
5353
./zz/graphCut/graph.h \
5454
./zz/abscutout.h \
5555
./zz/imagecut.h \
56-
./zz/StdAfx.h \
5756
./zz/GMM/CmFile.h \
5857
./zz/meanshiftSegmentation/allsegs.h \
5958
./zz/meanshiftSegmentation/cvHelper.h \
@@ -81,9 +80,12 @@ HEADERS += mainwindow.h \
8180
jsoncons/jsoncons_io.hpp \
8281
jsoncons/output_format.hpp \
8382
jsoncons/ovectorstream.hpp \
84-
jsoncons/parse_error_handler.hpp
83+
jsoncons/parse_error_handler.hpp \
84+
mywindow.h
8585

86-
LIBS += -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core -lopencv_hal
86+
LIBS += -L/usr/local/lib/ -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core -lopencv_imgcodecs
8787

88-
FORMS += mainwindow.ui
88+
FORMS += mainwindow.ui \
89+
mywindow.ui
8990
QMAKE_CXXFLAGS += -std=c++11
91+
#CONFIG += console debug

0 commit comments

Comments
 (0)