Counting all the number in a 2d array - c++

I want to count all the numbers in a 2d array and store the count into another array so can I can use the values in a histogram equalization.I am counting values that range from 0 to 255, so everytime a number for example 18 comes up in the 2d array I want to count how many 18's there are in the 2d array and then store the count into num[17]. Problem is I don't get the right amount. I know is due to the temp not being in the right place but I cannot figure out where to put it. Any help would be appreciated.
#include <iostream>
void histeq(int **pix, int height, int width) {
int num[255];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int temp = 0;
for (int k = 1; k <= 255; k++)
{
if (pix[i][j] == k)
{
temp = temp + 1;
}
num[k - 1] = temp;
cout << num[k - 1] << endl;
}
}
}
}

It's not very clear form your question, but my best guess is that you need this:
void histeq(int **pix, int height, int width) {
int num[256] = {0};
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
num[pix[i][j]] += 1;
}
}
for (int i = 0; i < 256; ++i)
{
cout << num[i] << endl;
}
}

#include <map>
void histeq(int **pix, int height, int width) {
std::map <int, int> num;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
num[pix[i][j]]++;
}
}
for (int i = 0; i < num.size(); i++)
cout << num[i] <<endl;
}

I got your expectation. You mean you want to count how many time number of arange[0:255] in 2d Array:
#include <iostream>
void histeq(int **pix, int height, int width) {
int num[255];
for (int k =1; k <= 255; k++)
{
int temp = 0;
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
if (pix[i][j] == k)
{
temp +=1;
}
}
}
num[k - 1] = temp;
cout << num[k - 1] << endl;
}
}

Related

Return matrix from function with variable lenght

I want to return the matrix from the function, but I can't find a way how. I've found some ways, but they can't be used for VLA. I've read about using std::vector, but that also didn't work.
int gengrid(int gridsize)
{
gridsize = 10 - 1;
int grid[gridsize+3][gridsize+3];
srand(time(NULL));
int count = 0;
std::fill_n(grid[0], 12, 0);
for(int i = 1; i < gridsize + 2; i++)
{
grid[i][0] = 0;
for(int j = 1; j < gridsize + 2; j++)
{
grid[i][j] = rand()%2;
}
grid[i][gridsize+2] = 0;
}
std::fill_n(grid[gridsize+2], gridsize + 3, 0);
return grid;
}
Okay, I found out my solution.
I initialize vector matrix with
static std::vector<std::vector<int>> grid(gridsize+3, std::vector<int>(gridsize+3));
which sets 0 by default for all elements.
(honestly, I don't know, how it's working, maybe somebody would comment explanation of this behavior.)
Complete code here:
#include <iostream>
#include <time.h>
#include <vector>
std::vector<std::vector<int>> gengrid(int gridsize)
{
gridsize = 10 - 1;
static std::vector<std::vector<int>> grid(gridsize+3, std::vector<int>(gridsize+3));//[gridsize+3][gridsize+3];
srand(time(NULL));
int count = 0;
for(int i = 1; i < gridsize + 2; i++)
{
grid[i][0] = 0;
for(int j = 1; j < gridsize + 2; j++)
{
grid[i][j] = rand()%2;
}
grid[i][gridsize+2] = 0;
}
return grid;
}
int main()
{
std::vector<std::vector<int>> grid = gengrid(10);
for(int i = 0; i < 9 + 3; i++)
{
for(int j = 0; j < 9 + 3; j++)
{
std::cout << grid[i][j];
}
std::cout << std::endl;
}
return 0;
}

Find the index of the largest element

The problem with my code is that it is not identifying my function, I am not sure if the function is incorrect or written with the wrong syntax. What I have tried is to create a new array for the location of the largest index but it doesn't seem to work.
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(const double a[][4], int location[]);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main(){
int location [ROW_SIZE][COLUMN_SIZE];
double matrix [ROW_SIZE][COLUMN_SIZE];
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
locateLargest(matrix, location)
}
You can keep track of the max value's indices while iterating through the matrix.
void max_idx(const double (&arr)[RS][CS]) {
double curr_max = arr[0][0];
size_t max_i = 0, max_j = 0;
for (size_t i = 0; i < RS; ++i) {
for (size_t j = 0; j < CS; ++j) {
if (curr_max < arr[i][j]) {
curr_max = arr[i][j];
max_i = i;
max_j = j;
}
}
}
cout << "Largest value is at (i=" << max_i << ", j=" << max_j << ")\n";
}
Demo
First of all, you have to make sure that your code is consistent : in the prototype of your locateLargest function, location is a one-dimensional array but in your main() function it is a two-dimensional one.
This is how I would write this :
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(double** a, int* location);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main()
{
int location [2];
double* matrix [ROW_SIZE];
for(int s= 0; s< ROW_SIZE; s++)
{
matrix[s]= new double[COLUMN_SIZE];
}
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++)
{
for(int j = 0; j < COLUMN_SIZE; j++)
{
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++)
{
for(int j = 0; j < COLUMN_SIZE; j++)
{
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
locateLargest(matrix, location);
}
void locateLargest(double** a, int* location)
{
int i, j;
double maxVal= a[0][0]; location[0]= location[1]= 0;
for(i = 0;i < ROW_SIZE; i++)
{
for(j = 0; j < COLUMN_SIZE; j++)
{
if(maxVal < a[i][j])
{
location[0] = i;
location[1]= j;
maxVal= a[i][j];
}
}
}
cout << "The location of the largest element is at ("<< location[0] << " , "<<
location[1] <<" ) . it is : "<< maxVal<<endl;
}
max represents the maximum value of your matrix's elements, you first set it to be equal to the first element and then compare it to each element of the matrix. Each time you find an element that is larger than max, you assign his value to max and his position to location and at the end of the iterations, you have the largest value and his location.

multiply two negative numbers in c++

when I tried to multiple two negative numbers the value it is zero in c++,
for example -5 * -3
the result is zero,
why?
this is my code
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void Multiply(const int v_arr[], const int m_arr[][3], int signed
o_arr[], int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
o_arr[i] = 0;
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
}
//End your code here
}
int main()
{
int n;
cin >> n;
int v_array[n];
int m_array[n][3];
int signed o_array[3];
for (int i = 0; i < n; i++) {
cin >> v_array[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> m_array[i][j];
}
}
//fuction
Multiply(v_array, m_array, o_array, n);
for (int j = 0; j < 3; j++) {
cout << o_array[j] << " ";
}
return 0;
}
how to fix it to get the correct result?
the input is
2
2 -3
2 -3
2 -4
Your issue is here:
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
You access elements at indices 0, 1 and 2 in v_arr, but it only has 2 elements. That's Undefined Behaviour.
Assuming this is matrix*vector multiplication code, it should look like this (untested):
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
Also, your loop based on j is useless. You can remove it:
void Multiply(const int v_arr[], const int m_arr[][3], int signed o_arr[], int size)
{
for(int k = 0; k < 3; k++) { //initialize output array
o_arr[k] = 0;
}
for (int i = 0; i < size; i++) {
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
}

How to find unique labels for segments in SuperpixelSLIC

I am using cv::ximgproc::SuperpixelSLIC opencv c++ to generate segments of image. I want each segment label to be unique. Here is my code.
Mat segmentImage() {
int num_iterations = 4;
int prior = 2;
bool double_step = false;
int num_levels = 10;
int num_histogram_bins = 5;
int width, height;
width = h1.size().width;
height = h1.size().height;
seeds = createSuperpixelSLIC(h1);
Mat mask;
seeds->iterate(num_iterations);
Mat labels;
seeds->getLabels(labels);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) == 0)
cout << i << " " << j << " " << labels.at<int>(i, j) << endl;
}
}
ofstream myfile;
myfile.open("label.txt");
myfile << labels;
myfile.close();
seeds->getLabelContourMask(mask, false);
h1.setTo(Scalar(0, 0, 255), mask);
imshow("result", h1);
imwrite("result.png", h1);
return labels;
}
In label.txt file I observe that label 0 has been given to two segments (i.e. segment include pixel(0,0) and pixel(692,442). These two segments are pretty far away.
Is this normal thing or my code is incorrect. Please help me to find unique label for each segment.
What you essentially need is a connected components algorithm. Without knowing the exact SLIC implementation you use, SLIC usually tends to produce disconnected superpixels, i.e. disconnected segments with the same label. A simple solution I used is the connected components algorithm form here: https://github.com/davidstutz/matlab-multi-label-connected-components (originally from here: http://xenia.media.mit.edu/~rahimi/connected/). Note that this repository contains a MatLab wrapper. In your case you only need connected_components.h together with the following code:
#include "connected_components.h"
// ...
void relabelSuperpixels(cv::Mat &labels) {
int max_label = 0;
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) > max_label) {
max_label = labels.at<int>(i, j);
}
}
}
int current_label = 0;
std::vector<int> label_correspondence(max_label + 1, -1);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
int label = labels.at<int>(i, j);
if (label_correspondence[label] < 0) {
label_correspondence[label] = current_label++;
}
labels.at<int>(i, j) = label_correspondence[label];
}
}
}
int relabelConnectedSuperpixels(cv::Mat &labels) {
relabelSuperpixels(labels);
int max = 0;
for (int i = 0; i < labels.rows; ++i) {
for (int j = 0; j < labels.cols; ++j) {
if (labels.at<int>(i, j) > max) {
max = labels.at<int>(i, j);
}
}
}
ConnectedComponents cc(2*max);
cv::Mat components(labels.rows, labels.cols, CV_32SC1, cv::Scalar(0));
int component_count = cc.connected<int, int, std::equal_to<int>, bool>((int*) labels.data, (int*) components.data, labels.cols,
labels.rows, std::equal_to<int>(), false);
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
labels.at<int>(i, j) = components.at<int>(i, j);
}
}
// component_count would be the NEXT label index, max is the current highest!
return component_count - max - 1;
}
On the obtained labels, run relabelConnectedSuperpixels.

Error in my code - Boolean Truth Table

I am currently working on a program that prints a 5 variable truth table. I am using a 2d array. My code currently produces the table, but says it is corrupt and "the stack around the variable "table" was corrupted. Any help?
#include <iostream>
using namespace std;
int main() {
bool table[5][32];
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
This is homework, so I would like to understand it, not just have an answer.
The index is wrong. Only table[0] to table[4] are available, so accessing table[5] to table[31] is illegal.
Try this:
#include <iostream>
using namespace std;
int main() {
bool table[32][5]; // swap 32 and 5
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
There is attempt to read out of bound values from array.
If you need 5x32 matrix Use code below:
for (int i = 0; i < 5; i++) { // 32-> 5
for (int j = 0; j < 32; j++) { // 5->32
If you need 32x5 matrix then replace code below:
bool table[32][5]; //it was table[5][32];