Skip to content

Commit 91f2d4b

Browse files
committed
propagate
1 parent a47f450 commit 91f2d4b

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

main.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ int main(int argc, char *argv[])
103103
if (cmdstr == "load-region") {
104104
json data = ctl.load_region(cmd["data"]);
105105
res["data"].set("return", data);
106+
} else
107+
if (cmdstr == "drawmask") {
108+
json data = ctl.drawmask(cmd["data"]);
109+
res["data"].set("return", data);
110+
} else
111+
if (cmdstr == "loadmask") {
112+
json data = ctl.loadmask(cmd["data"]);
113+
res["data"].set("return", data);
106114
} else {
107115
res["status"] = "error";
108116
res["error"] = string("command '")+cmdstr+("' not found");

multilevel.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,77 @@ json MultilevelController::paint(json data) {
11511151
return res;
11521152
}
11531153

1154+
json MultilevelController::drawmask(json data) {
1155+
json contours_json = data["from"]["marks"][0]["contours"];
1156+
vector< vector<Point> > contours;
1157+
contours.resize(contours_json.size());
1158+
for (int i=0; i<(int)contours.size(); i++) {
1159+
json tmp = contours_json[i];
1160+
if (tmp.size() && tmp[0].has_member("d")) {
1161+
Point pp(tmp[tmp.size()-1]["x"].as<double>(), tmp[tmp.size()-1]["y"].as<double>());
1162+
for (int j=0; j<(int)tmp.size(); j++) {
1163+
Point p(tmp[j]["x"].as<double>(),tmp[j]["y"].as<double>());
1164+
contours[i].push_back(p);
1165+
}
1166+
} else {
1167+
for (int j=0; j<(int)tmp.size(); j++)
1168+
contours[i].push_back(Point(tmp[j]["x"].as<double>(),tmp[j]["y"].as<double>()));
1169+
}
1170+
}
1171+
QPainterPath cutSelectPath = QPainterPath();
1172+
for (int i = 0; i < (int)contours.size(); i++) {
1173+
cutSelectPath.moveTo(QPoint(contours[i][0].x, contours[i][0].y));
1174+
for (vector<Point>::iterator it = contours[i].begin(); it != contours[i].end(); it++)
1175+
cutSelectPath.lineTo(QPoint((*it).x, (*it).y));
1176+
cutSelectPath.lineTo(QPoint(contours[i][0].x, contours[i][0].y));
1177+
}
1178+
QImage qStrokeMask = QImage(image.w, image.h, QImage::Format_RGB32);
1179+
qStrokeMask.fill(0);
1180+
QPainter pt;
1181+
pt.begin(&qStrokeMask);
1182+
pt.setBrush(Qt::white);
1183+
pt.setPen(Qt::NoPen);
1184+
pt.drawPath(cutSelectPath);
1185+
qStrokeMask.save((data["tmpdir"].as<string>()+"/2.png").c_str());
1186+
cerr << "Write mask to : " << (data["tmpdir"].as<string>()+"/2.png") << endl;
1187+
1188+
json res = json::object();
1189+
return res;
1190+
}
1191+
json MultilevelController::loadmask(json data) {
1192+
string img_name = data["tmpdir"].as<string>()+"/3.png";
1193+
cerr << "Load mask from : " << img_name << endl;
1194+
QImage mask(img_name.c_str());
1195+
MyImage image(mask.width(), mask.height());
1196+
for (int y=0; y<image.h; y++) {
1197+
QRgb* scanelineStrokeMask = (QRgb*)mask.scanLine(y);
1198+
for (int x=0; x<image.w; x++) {
1199+
int grayValue = qGray(scanelineStrokeMask[x]);
1200+
image.get(x,y) = 0;
1201+
if (grayValue>100) {
1202+
image.get(x,y) = 255;
1203+
}
1204+
}
1205+
}
1206+
vector<vector<pair<int,int>>> contours;
1207+
findContours(image, contours, true);
1208+
json arr1 = json::array();
1209+
for (int i=0; i<(int)contours.size(); i++) {
1210+
json arr2 = json::array();
1211+
for (int j=0; j<(int)contours[i].size(); j++) {
1212+
json point;
1213+
pair<int,int> p = contours[i][j];
1214+
point["x"] = p.first;
1215+
point["y"] = p.second;
1216+
arr2.add(point);
1217+
}
1218+
arr1.add(arr2);
1219+
}
1220+
json res = json::object();
1221+
res.set("contours", arr1);
1222+
return res;
1223+
}
1224+
11541225
json MultilevelController::load_region(json data) {
11551226
lock_guard<mutex> lg(selection_lock);
11561227
{

multilevel.h

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class MultilevelController : public QObject {
101101
json load_base64(string);
102102
json paint(json);
103103
json load_region(json);
104+
json drawmask(json);
105+
json loadmask(json);
104106

105107
EdgeSeg fgEdgeSeg, bgEdgeSeg;
106108

0 commit comments

Comments
 (0)