write image providing vector of coordinates

I have store some found pixel coordinates in a vector. Here, I am trying to write a new image, and paint any colour to its pixel coordinates (that matches the one in vector). I am not sure how to access the coordinates stored in my vector.

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;
}


Can anybody guide me on my code? Thank you
Last edited on
myVec(Point(r, c))

myVec is just a simple vector, so you access elements like any other vector:

Point myPoint = myVec[index]; // index is an integer
Thank you MikeyBoy for your reply.

I've tried the following,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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++)
			{
				for (vector<Point>::const_iterator z= myVec.begin(); z!=  myVec.end(); z++)
				{
					if (tryy.at<cv::Vec3b>(k, l) == myVec[z])// 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;
					}
				}
			}
		}


Here, I've got errors, "no operator"[]" matches these operands" on the myVec[index].
Would you shed me some light?
Simple answer: an iterator is not an index. Iterators use similar semantics as pointers, so to get the value an iterator is "pointing" to, you use the dereference operator. So:

if (tryy.at<cv::Vec3b>(k, l) == *z)
I've change the line to :

 
if (tryy.at<cv::Point>(k, l) == *z)


I've change it to 'Point' instead of 'Vec3b' as it is giving me error that i think caused by the different type of cv.

Now that it has no error, but I have some unhandled exception of assertion error.

Is it even possible to get pixel coordinate from a binary image then paint image following those coordinates? because it doesn't seems to be working until now. Thanks :)
Topic archived. No new replies allowed.