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
|
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include <vector>
#include <direct.h>
#include <algorithm> // for copy
#include <iterator> // for ostream_iterator
using namespace cv;
using namespace std;
Mat pic, gray, bnw;
int rows, cols;
string path_in = "C:/Users/Dell/Desktop/";
string path_out = "C:/Users/Dell/Desktop/New Folder/";
int main(int argc, char** argv)
{
vector<cv::String> fn;
vector<cv::String> fo;
glob(path_in + "*.jpg", fn, false);
glob(path_out + "*.jpg", fo, false);
size_t count = fn.size();
for (size_t i = 1; i < 2; i++) //try for an image first, if works, then try for all images in folder
{
pic = imread(fn[i], CV_LOAD_IMAGE_COLOR);
namedWindow("Before", 1);
cv::imshow("Before", pic);
cvtColor(pic, gray, CV_BGR2GRAY);
threshold(gray, bnw, 128.0, 255.0, THRESH_BINARY);
imshow("gray", gray);
std::vector<Point> myVec;
for (int r = 0; r < bnw.rows; r++) {
for (int c = 0; c < bnw.cols; c++) {
if (bnw.at<uchar>(r, c) > 0) { //white pixel coordinate
{
myVec.push_back(Point(r, c));
}
}
}
Mat tryy(480, 640, CV_8UC3, Scalar(255, 255, 255)); //scalar is the initial value of each pixel
for (int k = 0; k < tryy.rows; k++)
{
for (int l = 0; l < tryy.cols; l++)
{
// problem is here i am not sure how to write this part of code
if (tryy.at<cv::Vec3b>(k, l) == myVec(Point(r, c))// Pixel isn't black
{
Vec3b color = tryy.at<Vec3b>(Point(k, l)); //if match pixel coordinate, paint those coordinates in new img
color.val[0] = 255;
color.val[1] = 0;
color.val[2] = 0;
tryy.at<Vec3b>(Point(k, l)) = color;
}
}
}
imshow("now", tryy);
imwrite(fo[i], tryy);
waitKey(0);
}
waitKey();
//system("PAUSE");
return 0;
}
|