Boost graph library breadth first search yielding incorrect predecessor map - breadth-first-search

Running breadth-first search on an unweighted, directed graph on 2 vertices where each vertex is connected to the other yields a predecessor map where the source of the breadth-first search is not its own predecessor. The following program is sufficient to produce this behavior:
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
using namespace boost;
using std::vector;
enum family { one, two, N };
typedef adjacency_list< vecS, vecS, directedS> Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
int main() {
Graph g(N);
const char *name[] = { "one", "two" };
add_edge(one, two, g);
add_edge(two, one, g);
vector<Vertex> p(num_vertices(g));
breadth_first_search(g, two, visitor(make_bfs_visitor(
record_predecessors(&p[0],
on_tree_edge()))));
//At this point, p[0] == 1 and p[1] == 0
return 0;
}
This seems to contradict the Boost Graph Library documentation. More importantly, the predecessor map should represent a spanning tree of the graph breadth-first search is run on, which is not the case when the source of the search is not its own predecessor.

Related

Contiki find neighbors

I want to find or list all of my neighbor nodes. It should be broadcast or unicast process for nodes. How can I find them with Contiki? Are there any functions for that?
IPv6 neighbors are stored in list ds6_neighbors. To iterate over this list you can use this code:
For Contiki:
#include "net/ipv6/uip-ds6.h"
uip_ds6_nbr_t *nbr;
for(nbr = nbr_table_head(ds6_neighbors);
nbr != NULL;
nbr = nbr_table_next(ds6_neighbors, nbr)) {
/* process nbr here */
}
For Contiki-NG:
#include "net/ipv6/uip-ds6-nbr.h"
uip_ds6_nbr_t *nbr;
for(nbr = uip_ds6_nbr_head();
nbr != NULL;
nbr = uip_ds6_nbr_next(nbr)) {
/* process nbr here */
}
Other network layers have their own notions of neighbors. There are TSCH neighbors, RPL neighbors (called "parents"), and link layer neighbors, each in a separate list.

Read cv::Mat pixel without knowing its pixel format

I am aware there are several ways to read and write a pixel value of an OpenCV cv::Mat image/matrix.
A common one is the .at<typename T>(int, int) method http://opencv.itseez.com/2.4/modules/core/doc/basic_structures.html#mat-at .
However, this requires the typename to be known, for instance .at<double>.
The same thing applies to more direct pointer access OpenCV get pixel channel value from Mat image .
How can I read a pixel value without knowing its type? For instance, it would be ok to receive a more generic CvScalar value in return. Efficiency is not an issue, as I would like to read rather small matrices.
Kind of. You can construct cv::Mat_ and provide explicit type for elements, after that you don't have to write element type each time. Quoting opencv2/core/mat.hpp
While Mat is sufficient in most cases, Mat_ can be more convenient if you use a lot of element
access operations and if you know matrix type at the compilation time. Note that
Mat::at(int y,int x) and Mat_::operator()(int y,int x) do absolutely the same
and run at the same speed, but the latter is certainly shorter.
Mat_ and Mat are very similar. Again quote from mat.hpp:
The class Mat_<_Tp> is a thin template wrapper on top of the Mat class. It does not have any
extra data fields. Nor this class nor Mat has any virtual methods. Thus, references or pointers to
these two classes can be freely but carefully converted one to another.
You can use it like this
Mat_<Vec3b> dummy(3,3);
dummy(1, 2)[0] = 10;
dummy(1, 2)[1] = 20;
dummy(1, 2)[2] = 30;
cout << dummy(1, 2) << endl;
Why I said 'kind of' in the first place? Because if you want to pass this Mat_ somewhere - you have to specify it's type. Like this:
void test(Mat_<Vec3b>& arr) {
arr(1, 2)[0] = 10;
arr(1, 2)[1] = 20;
arr(1, 2)[2] = 30;
cout << arr(1, 2) << endl;
}
...
Mat_<Vec3b> dummy(3,3);
test(dummy);
Technically, you are not specifying your type during a pixel read, but actually you still have to know it and cast the Mat to the appropriate type beforehand.
I guess you can find a way around this using some low-level hacks (for example make a method that reads Mat's type, calculates element size and stride, and then accesses raw data using pointer arithmetic and casting...). But I don't know any 'clean' way to do this using OpenCV's functionality.
If you already know the type, you can use Mat_<> type for easy access. If you don't know the type, you can:
convert the data to double, so data won't be truncated in any case
switch over the number of channels to access correctly the double matrix. Note that you can have at most of 4 channels, since Scalar has at most 4 elements.
The following code will convert only the selected element of the source matrix to a double value (with N channels).
You get a Scalar containing the value at position row, col in the source matrix.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Scalar valueAt(const Mat& src, int row, int col)
{
Mat dst;;
src(Rect(col, row, 1, 1)).convertTo(dst, CV_64F);
switch (dst.channels())
{
case 1: return dst.at<double>(0);
case 2: return dst.at<Vec2d>(0);
case 3: return dst.at<Vec3d>(0);
case 4: return dst.at<Vec4d>(0);
}
return Scalar();
}
int main()
{
Mat m(3, 3, CV_32FC3); // You can use any type here
randu(m, Scalar(0, 0, 0, 0), Scalar(256, 256, 256, 256));
Scalar val = valueAt(m, 1, 2);
cout << val << endl;
return 0;
}

Get/Modify single entries in non-contiguous submatrix views

I would like to access and modify single entries in a non-contiguous submatrix view. I tried it like this:
#include <armadillo> // version 5.200.2
int main()
{
arma::mat A(4, 4, arma::fill::zeros);
arma::uvec b(4);
b << 2 << 3;
auto view = A.elem(b, b);
view(0, 0) = 1.0; // Error: No operator()
}
This doesn't work because the expression returned by A.elem(b, b) appears to have no operator() defined. I found that the same thing works with contiguous views like e.g. submat(). Is there any solution/workaround for this or is it simply not possible in the non-contiguous case?

Can we create n channel image in opencv,n will be around 20

Presently Iam working in finding disparity of stereo pair. I have got a situation in creating 20 channel data set, When I declare array of 3 dimension it was giving error, Instead can I create image of 20 channels so that I can store data. If I can what are the additional conditions I have to include to get results without any error of memory allocation or sort of .... Creating an Image of 20 channels will be even comfortable for me...
The C++ interface of OpenCV presents cv::Mat, which replaces and improves the IplImage type of the C interface. This new type provides several constructors, including the one below which can be used to specify the desired number of channels through the param type:
Mat::Mat(int rows, int cols, int type)
Sample code:
#include <cv.h>
#include <highgui.h>
#include <iostream>
void test_mat(cv::Mat mat)
{
std::cout << "Channels: " << mat.channels() << std::endl;
}
int main(int argc, char* argv[])
{
cv::Mat mat20(1024, 768, CV_8UC(20));
test_mat(mat20);
return 0;
}
Opencv implements template class for small matrices whose type and size are known at compilation time:
template<typename _Tp, int m, int n> class Matx {...};
You can create a specified template of a partial case of Matx, which is cv::Vec like those already written in opencv for 1,2, or 3 "channels" like that:
typedef Vec<uchar, 3> Vec3b; // 3 channel -- written in opencv
typedef Vec<uchar, 20> Vec20b; // the one you need
And then, declare a Matrix of your new (20 channel of uchar) object:
cv::Mat_<Vec20b> myMat;
myMat.at<Vec20b>(i,j)(10) = .. // access to the 10 channel of pixel (i,j)

What this cast and assignment is all about?

I am reading Richard Stevens' Advance Programming in unix environment.
There is a code in thread synchronization category (chapter - 11).
This is code showing is showing how to avoid race conditions for many shared structure of same type.
This code is showing two mutex for synch.- one for a list fh (a list which keep track of all the foo structures) & f_next field and another for the structure foo
The code is:
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define NHASH 29
#define HASH(fp) (((unsigned long)fp)%NHASH)
struct foo *fh[NHASH];
pthread_mutex_t hashlock = PTHREAD_MUTEX_INITIALIZER;
struct foo {
int f_count;
pthread_mutex_t f_lock;
struct foo *f_next; /* protected by hashlock */
int f_id;
/* ... more stuff here ... */
};
struct foo * foo_alloc(void) /* allocate the object */
{
struct foo *fp;
int idx;
if ((fp = malloc(sizeof(struct foo))) != NULL) {
fp->f_count = 1;
if (pthread_mutex_init(&fp->f_lock, NULL) != 0) {
free(fp);
return(NULL);
}
idx = HASH(fp);
pthread_mutex_lock(&hashlock);
///////////////////// HERE -----------------
fp->f_next = fh[idx];
fh[idx] = fp->f_next;
//////////////////// UPTO HERE -------------
pthread_mutex_lock(&fp->f_lock);
pthread_mutex_unlock(&hashlock);
/* ... continue initialization ... */
pthread_mutex_unlock(&fp->f_lock);
}
return(fp);
}
void foo_hold(struct foo *fp) /* add a reference to the object */
.......
The doubt is
1) What is HASH(fp) pre-processor doing?
I know that it is typecasting what is fp store and then taking its modulo. But, in the function foo_alloc we are just passing the address of newly allocated foo structure.
Why we are doing this I know that this will give me a integer between 0 and 28 - to store in array fh. But why are we taking modulo of an address. Why there is so much randomization?
2) Suppose i accept that, now after this what these two lines are doing (also highlighted in the code) :
fp->f_next = fh[idx];
fh[idx] = fp->f_next;
I hope initially fh[idx] has any garbage value which i assigned to the f_next field of foo and in the next line what is happening , again the same assignment but in opposite order.
struct foo *fh[NHASH] is a hash table, and use the HASH macro as the hash function.
1) HASH(fp) calculates the index to decide where the in the fh to store fp, and it uses the address of the fp and uses the address as key to calculate the index. We can easily typecast the address to the long type.
2) Use the linked list to avoid the hash collisions called separate chaining, and I think the following cod is right, and you can check it in the book :
fp->f_next = fh[idx];
fh[idx] = fp;
insert the fp element to the header of the linked list fh[idx], and the initial value of the fh[idx] is null.

Resources