cpp:



#include "cv.h"  
#include "highgui.h"
#include <iostream>
#include <fstream>
#include <string.h>


using namespace std;
using namespace cv;

int main()
{
ifstream infile;
infile.open("/media/hdc/xing/DeepLabV2/dataset/cityspace/gtFine/gtFine_color.txt");
string tmp;
while(getline(infile,tmp)){

string name = tmp.substr(2);
// cout << name << endl;
string gt_dir = "/media/hdc/xing/DeepLabV2/dataset/cityspace/gtFine/" + name;
string dis_dir = "/home/bnrc/cityscape_123label/" + name;
// cout << gt_dir << endl;
// cout << dis_dir << endl;

Mat img;
img = imread(gt_dir);
int col = img.cols;
int row = img.rows;
// cout << col << endl;
// cout << row << endl;
//Mat result(1024, 2048, CV_8UC1, Scalar(255));
Mat result(1024, 2048, CV_8UC1, Scalar(0));
for(int i = 0;i < col;i++){
int index = 1024;
int blue = img.at<Vec3b>(index-1,i)[0];
int green = img.at<Vec3b>(index-1,i)[1];
int red = img.at<Vec3b>(index-1,i)[2];
while(index && (blue != 128 || green != 64 || red != 128 )){
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
while(index && blue == 128 && green == 64 && red == 128){
// result.at<uchar>(index-1,i) = 0;
result.at<uchar>(index-1,i) = 1;
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
int blue1 = blue;
int green1 = green;
int red1 = red;
while(index && blue == blue1 && green == green1 && red == red1){
// result.at<uchar>(index-1,i) = 150;
result.at<uchar>(index-1,i) = 2;
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
while(index && (blue != 128 || green != 64 || red != 128 )){
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
while(index && blue == 128 && green == 64 && red == 128){
// result.at<uchar>(index-1,i) = 0;
result.at<uchar>(index-1,i) = 1;
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
int blue2 = blue;
int green2 = green;
int red2 = red;
while(index && blue == blue2 && green == green2 && red == red2){
// result.at<uchar>(index-1,i) = 150;
result.at<uchar>(index-1,i) = 2;
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
}
// delete car flag
for(int i = 0;i < col;i++){
for(int j = 0;j < row;j++){
int blue = img.at<Vec3b>(j,i)[0];
int green = img.at<Vec3b>(j,i)[1];
int red = img.at<Vec3b>(j,i)[2];
// cout << blue << " " << green << " " << red << ":" << i << " " << j << endl;
if(blue == 0 && green == 0 && red == 0){
// result.at<uchar>(j,i) = 255;
result.at<uchar>(j,i) = 0;
}
}
}
imwrite(dis_dir,result);
}



//method 2

/*string gt_dir = "/media/hdc/xing/DeepLabV2/dataset/cityspace/gtFine/aachen/aachen_000001_000019_gtFine_color.png";
// size_t pos = tmp.find("/");
// string str = tmp.substr(0,pos);
// cout << str << endl;
string dis_dir = "/home/bnrc/aachen_000007_000019_gtFine_color.png";
// cout << gt_dir << endl;
// cout << dis_dir << endl;
Mat img;
img = imread(gt_dir);
int col = img.cols;
int row = img.rows;
// cout << col << endl;
// cout << row << endl;
Mat result(1024, 2048, CV_8UC1, Scalar(255));
for(int i = 0;i < col;i++){
int index = 1024;
int blue = img.at<Vec3b>(index-1,i)[0];
int green = img.at<Vec3b>(index-1,i)[1];
int red = img.at<Vec3b>(index-1,i)[2];
while(index && (blue != 128 || green != 64 || red != 128 )){
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
while(index && blue == 128 && green == 64 && red == 128){
result.at<uchar>(index-1,i) = 0;
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
int blue1 = blue;
int green1 = green;
int red1 = red;
while(index && blue == blue1 && green == green1 && red == red1){
result.at<uchar>(index-1,i) = 150;
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
while(index && (blue != 128 || green != 64 || red != 128 )){
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
while(index && blue == 128 && green == 64 && red == 128){
result.at<uchar>(index-1,i) = 0;
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
int blue2 = blue;
int green2 = green;
int red2 = red;
while(index && blue == blue2 && green == green2 && red == red2){
result.at<uchar>(index-1,i) = 150;
index--;
blue = img.at<Vec3b>(index-1,i)[0];
green = img.at<Vec3b>(index-1,i)[1];
red = img.at<Vec3b>(index-1,i)[2];
}
}
imwrite(dis_dir,result);
*/



//method 1

/*ifstream infile;
string gt_dir = "/media/hdc/xing/DeepLabV2/dataset/cityspace/gtFine/aachen/aachen_000007_000019_gtFine_labelIds.png";
string dis_dir = "/home/bnrc/aachen_000001_000019_gtFine_labelIds.png";
Mat img;
img = imread(gt_dir);
int col = img.cols;
int row = img.rows;
cout << col << endl;
cout << row << endl;
Mat result(1024, 2048, CV_8UC1, Scalar(255));
for(int i = 0;i < col;i++){
int index = 1024;
int label = img.at<uchar>(index-1,i);
// cout << label << endl;
while(index && label != 7){
index--;
label = img.at<uchar>(index-1,i);
// cout << label << endl;
}
// cout << label << endl;
while(index && label == 7){
result.at<uchar>(index-1,i) = 0;
index--;
label = img.at<uchar>(index-1,i);
// cout << index << ' ' <<label << endl;
}
// int value = label;
// while(index && label == value){
// result.at<uchar>(index-1,i) = 150;
// index--;
// label = img.at<uchar>(index-1,i);
// // cout << label << endl;
// }
}
imwrite(dis_dir,result);*/
return 0;
}


 

python:



import cv2
import numpy as np




with open('/media/hdc/xing/data_semantics/training/semantic_rgb/a.txt','r') as file:
# num = 0
for line in file:
# if num == 1:
# break
# num += 1
gt_image = '/media/hdc/xing/data_semantics/training/semantic_rgb/' + line.strip().split('./')[1]
# print gt_image
save_dir = '/media/hdc/xing/data_semantics/training/label012/' + line.strip().split('./')[1]
img_gt = cv2.imread(gt_image)
# print type(img_gt)
height = img_gt.shape[0]
width = img_gt.shape[1]
# print width,height
result = np.zeros((height,width))
for i in range(width):
index = height-1
red,green,blue = img_gt[index,i]
# print i,red,green,blue
while index >= 1 and (red != 128 or green != 64 or blue != 128):
index -= 1
red,green,blue = img_gt[index,i]
while index >= 1 and red == 128 and green == 64 and blue == 128:
index -= 1
result[index][i] = 1
# result[index][i] = 255
red,green,blue = img_gt[index,i]
while (index >= 1 and red == 232 and green == 35 and blue == 244) or (index >= 1 and red == 152 and green == 251 and blue == 152) or (index >= 1 and red == 160 and green == 170 and blue == 250) or (index >= 1 and red == 81 and green == 0 and blue == 81):
index -= 1
red,green,blue = img_gt[index,i]
while index >= 1 and red == 128 and green == 64 and blue == 128:
index -= 1
result[index][i] = 1
# result[index][i] = 255
red,green,blue = img_gt[index,i]
blue1 = blue
green1 = green
red1 = red
# print blue1,green1,red1
while index >= 1 and blue == blue1 and green == green1 and red == red1:
result[index][i] = 2
# result[index][i] = 150
index -= 1
red,green,blue = img_gt[index,i]
while index >= 1 and red == 128 and green == 64 and blue == 128:
index -= 1
result[index][i] = 1
# result[index][i] = 255
red,green,blue = img_gt[index,i]
while index >= 1 and blue == blue1 and green == green1 and red == red1:
result[index][i] = 2
# result[index][i] = 150
index -= 1
red,green,blue = img_gt[index,i]
# while index >= 1 and (red != 128 or green != 64 or blue != 128):
# index -= 1
# red,green,blue = img_gt[index,i]
# while index >= 1 and red == 128 and green == 64 and blue == 128:
# index -= 1
# # result[index][i] = 1
# result[index][i] = 255
# red,green,blue = img_gt[index,i]
# while index >= 1 and red == 250 and green == 170 and blue == 160:
# index -= 1
# red,green,blue = img_gt[index,i]
# while index >= 1 and red == 244 and green == 35 and blue == 232:
# index -= 1
# red,green,blue = img_gt[index,i]
# while index >= 1 and red == 152 and green == 251 and blue == 152:
# index -= 1
# red,green,blue = img_gt[index,i]
# blue2 = blue
# green2 = green
# red2 = red
# while index >= 1 and blue == blue2 and green == green2 and red == red2:
# index -= 1
# # result[index][i] = 2
# result[index][i] = 150
# red,green,blue = img_gt[index,i]
# print index
for i in range(width):
for j in range(height):
red,green,blue = img_gt[j,i]
if blue == 0 and green == 0 and red == 0:
result[j][i] = 0
cv2.imwrite(save_dir,result)
print save_dir