I am trying to rightshift all the pixels of a Image by 1 bit. I don't see any vectorized way to do this. As of now, the only solution that I have is as follows
void round_down(cv::Mat& image) {
int channels = image.channels();
int num_rows = image.rows;
int num_cols = (image.cols * channels);
if (image.isContinuous()) {
num_cols *= num_rows;
num_rows = 1;
}
for (int i = 0; i < num_rows; ++i) {
uchar* row_ptr = image.ptr<uchar>(i);
for (int j = 0; j < num_cols; ++j) {
row_ptr[j] >>= 1;
}
}
}
Are there any OpenCV C++ functions for bitwise leftshift and right shifts?
If not what is the best time optimized way to do it ?
Related
I have the following code:
void white(Mat&src,Mat&dst){
double h = src.cols;
double w = src.rows;
dst = src.clone();
for (int i = 0; i < dst.rows; i++) {
for (int j = 0; j < dst.cols; j++) {
dst.at<uchar>(i, j) = 255;
}
}
}
My expected result is to get a completely white image, however, I get this result:
Input Image:
Output Image:
It's a color image. Each pixel consists of 3 values. You have to set each color channel
void white(Mat&src,Mat&dst){
double h = src.cols;
double w = src.rows;
dst = src.clone();
for (int i = 0; i < dst.rows; i++) {
for (int j = 0; j < dst.cols; j++) {
for (std::size_t c = 0; c < 3; ++c) {
dst.at<Vec3b>(i, j)[c] = 255;
}
}
}
}
I am fairly new to C++ programming language. Currently, what I am trying to accomplish is getting a short* input from MATLAB, creating its transpose and multiplying. I am having problems when I try to multiply. I am getting h_raw from MATLAB and doing some computation to get sh_data. This looks correct. Next, I am creating a transpose dst which is also correct. Finding the covariance matrix is giving me troubles. This covariance matrix should be MATALB equivalent to cov_matrix=sh_data * sh_data'; Any help is appreciated!
for (int numEl = 0; numEl < elements; numEl++) {
int shift_idx = tof[1 + (1 * pix_x * numEl)];
for (int sdex = 0; sdex < shift_idx; sdex++) {
for (int p = 0; p < nrows; p++) {
sh_data[p + (numEl*nrows)] = h_raw[(p + sdex) + (numEl*nrows)];
}
}
}
// finding the transpose
for (int n = 0; n < nrows*ncols; n++) {
int i = n / ncols;
int j = n % ncols;
dst[n] = sh_data[nrows*j + i];
}
// calculating the covariance matrix
for (int nel = 0; nel < elements; nel++) {
for (int k = 0; k < nrows; k++) {
cov_matrix[(nel*elements)+nel] += dst[(k*elements)+nel] * sh_data[k+(nel*nrows)];
}
}
I have the following problem. I have written code for alpha-trimmed filter in opencv library. I think that it is properly constructed but I don't know how to compare two 3 channels pixels during sorting a 'window with pixels'. In my code it is done but comparing two but it is impossible for vectors. I assume that i should compare it one channel and after second and so on. Have you any hints for me, or could you propose some modifications in my code. This is my code.
int alphatrimmed(Mat img, int alpha)
{
Mat img9 = img.clone();
const int start = alpha;
const int end = 9 - alpha;
//going through whole image
for (int i = 1; i < img.rows - 1; i++)
for (int j = 1; j < img.cols-1; j++)
{
int k = 0;
Vec3b element[9];
//selecting elements
for (int m = i - 1; m < i + 2; m++)
for (int n = j - 1; n < j + 2; n++)
element[k++] = img.at<Vec3b>(m*img.cols + n);
for (int i = 0; i < end; i++)
{
int min = i;
for (int j = i + 1; j < 9; j++)
if (element[j] < element[min])
min = j;
Vec3b temp = element[i];
element[i] = element[min];
element[min] = temp;
}
const int result = (i - 1)*(img.cols - 2) + j - 1;
img9.at<Vec3b>(result) = element[start];
for (int j = start + 1; j < end; j++)
img9.at<Vec3b>(result) += element[j];
img9.at<Vec3b>(result) /= 9 - alpha;
}
namedWindow("AlphaTrimmed Filter", WINDOW_AUTOSIZE);
imshow("AlphaTrimmed Filter", img9);
return 0;
}
Thank you for your time spent on solving my problem.
If we access pixel by a pointer using step and data of Mat Image. see example below
int step = srcimg.step;
for (int j = 0; j < srcimg.rows; j++) {
for (int i = 0; i < srcimg.cols; i++) {
//this is pointer to the pixel value.
uchar* ptr = srcimg.data + step* j + i;
}
}
Question:
How can we perform 3x3 weighted avg operations with image step by a pointer?
thanks
You mustn't use data field in opencv because memory is not allways continuous. you can check this using isContinuous() method.
Now you can do like this (image type is CV_8UC1)
for (int i = 1; i < srcimg.rows-1; i++)
{
for (int j = 1; j < srcimg.cols-1; j++)
{
int x=0;
for (int k=-1;k<=1;k++)
{
uchar* ptr=srcimg.ptr(k+i)+j-1;
for (int l=-1;l<=1;l++,ptr++)
x +=*ptr;
}
}
}
image border are not processed. Now if you want to blur an image use blur method
You can use this post too
I am doing something like this .
int sr = 3;
for (int j = 0; j < srcimg.rows; j++) {
for (int i = 0; i < srcimg.cols; i++) {
uchar* cp_imptr = im.data;
uchar* tptr = im.data + imstep *(sr + j) + (sr + i);
int val_tptr = cp_imptr [imstep *(sr + j) + (sr + i)]; //pointer of image data amd step at 3x3
int val_cp_imptr = cp_imptr[imstep *j + i];
double s = 0;
for (int n = templeteWindowSize; n--;)
{
for (int m = templeteWindowSize; m--;)
{
uchar* t = tptr; //pointer of template
// sum
s += *t;
t++;
}
t += cstep;
}
}
cout << endl;
}
Is there a possibility to use a boost matrix along FFTW? if so how do you do it?
what i basically have is
QPixmap pixmap("lena.bmp");
// resize input image
pixmap = pixmap.copy(512,512,128,128);
pixmap = pixmap.scaled(128,128);
QImage image = pixmap.toImage();
QRgb col;
int g;
int width = pixmap.width();
int height = pixmap.height();
matrix<double> m(width,height);
for (int j = 0; j < m.size2(); j++)
{
for (int i = 0; i < m.size1(); i++)
{
m(i,j) = 0;
m(i,j) = qGray(image.pixel(i,j));
}
}
I want to perform FFTW on the matrix 'm' and then redisplay the fft of the image. how do i do this?
you can do is first read your image into an array, then apply the FFTW as per your needs on this array and then fill this array into a boost ublas matrix and then use it.
int a[width][height];
for (int j = 0; j < width; j++)
{
for (int i = 0; i < height; i++)
{
a[i][j] = qGray(image.pixel(i,j));
}
}
//apply fftw
matrix<double> m(width,height);
for (int j = 0; j < width; j++)
{
for (int i = 0; i < height; i++)
{
m(i,j) = fft_a[i][j];
}
}
I hope this works