-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_scene.cpp
executable file
·148 lines (131 loc) · 4.27 KB
/
graph_scene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "graph_scene.h"
#include <opencv2/imgproc/imgproc.hpp>
#include <QGraphicsSceneMouseEvent>
#include <QMessageBox>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
void GraphScreen::display(QImage* image){
ScreenPic = image;
this->addPixmap(QPixmap::fromImage(*image));
}
//move mouse event
void GraphScreen::mouseMoveEvent(QGraphicsSceneMouseEvent* event){
if (m_crop){
int mx = event->scenePos().x();
int my = event->scenePos().y();
Crop_m(mx,my);
this->addPixmap(QPixmap::fromImage(*ScreenPic));
}
}
void GraphScreen::mousePressEvent(QGraphicsSceneMouseEvent* event){
int mx = event->scenePos().x();
int my = event->scenePos().y();
this->addPixmap(QPixmap::fromImage(*ScreenPic));
if(m_addsticker){
add_sticker(mx, my, sticker_size);
this->addPixmap(QPixmap::fromImage(*ScreenPic));
}
else if (m_mosaic){
if (mx>0 && my>0 && mx<ScreenPic->size().width() && my<ScreenPic->size().width()){
mosaicPtL = Point(mx, my);
}
}
else if (m_text){
add_text(mx,my,text_size, text_content);
this->addPixmap(QPixmap::fromImage(*ScreenPic));
}
else if (m_crop){
if (mx>0 && my>0 && mx<ScreenPic->size().width() && my<ScreenPic->size().width()){
Crop_p(mx,my);
this->addPixmap(QPixmap::fromImage(*ScreenPic));
}
}
}
void GraphScreen::mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
int mx = event->scenePos().x();
int my = event->scenePos().y();
if(m_mosaic){
if (mx>0 && my>0 && mx<ScreenPic->size().width() && my<ScreenPic->size().width()){
mosaicPtR = Point(mx, my);
mosaic(mosaicPtL, mosaicPtR, mosaic_size);
this->addPixmap(QPixmap::fromImage(*ScreenPic));
}
}
else if (m_crop){
if (mx>0 && my>0 && mx<ScreenPic->size().width() && my<ScreenPic->size().width()){
Crop_r(mx,my);
this->display(ScreenPic);
set_m_crop(false);
}
}
}
//format change between QImage & cvMat
Mat GraphScreen::QImage2cvMat(QImage image) {
cv::Mat mat;
cv::Mat dst;
qDebug() << image.format();
switch(image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
image = image.convertToFormat(QImage::Format_RGB888);
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, dst, COLOR_BGR2RGB);
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, dst, COLOR_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return dst;
}
QImage GraphScreen::cvMat2QImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if(mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for(int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for(int row = 0; row < mat.rows; row ++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if(mat.type() == CV_8UC3)
{
// Copy input Mat
cvtColor(mat, mat,COLOR_BGR2RGB);
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image;
}
else if(mat.type() == CV_8UC4)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
return QImage();
}
}