Working with matrices, accelerate framework, iOS - ios

I have two matrices: A and B.
How can I store them?
How can I calculate the inverse matrix of matrix A using the Accelerate framework?
How can find the product of A*B?
How can I transpose matrix A using the Accelerate framework?
Thank you for answering my questions!
Helper file
#import <Foundation/Foundation.h>
#include <Accelerate/Accelerate.h>
#interface Working_with_matrices : NSObject
-(int)invert_matrix:(int) N andWithMatrix:(double*) matrix;
#end
Implementation file
#import "Working_with_matrices.h"
#include <Accelerate/Accelerate.h>
#implementation Working_with_matrices
-(int) matrix_invert:(int) N andWithMatrix:(double*)matrix
{
int error=0;
int *pivot = malloc(N*N*sizeof(int));
double *workspace = malloc(N*sizeof(double));
dgetrf_(&N, &N, matrix, &N, pivot, &error);
if (error != 0) {
NSLog(#"Error 1");
return error;
}
dgetri_(&N, matrix, &N, pivot, workspace, &N, &error);
if (error != 0) {
NSLog(#"Error 2");
return error;
}
free(pivot);
free(workspace);
return error;
}
Call my code from main function
#import <Foundation/Foundation.h>
#import "Working_with_matrices.h"
int main(int argc, const char * argv[])
{
int N = 3;
double A[9];
Working_with_matrices* wm=[[Working_with_matrices alloc]init];
A[0] = 1; A[1] = 1; A[2] = 7;
A[3] = 1; A[4] = 2; A[5] = 1;
A[6] = 1; A[7] = 1; A[8] = 3;
[wm invert_matrix:N andWithMatrix:A];
// [ -1.25 -1.0 3.25 ]
// A^-1 = [ 0.5 1.0 -1.5 ]
// [ 0.25 0.0 -0.25 ]
for (int i=0; i<9; i++)
{
NSLog(#"%f", A[i]);
}
return 0;
}

I'm still kinda new to using the accelerate framework but I'll answer what I can.
The accelerate framework expects the matrices to be passed in as a
1D array. So if you have a 4x4 matrix, the first row would be placed
in indexes 0-3 of your array, the second rouw would be placed in
indexes 4-7 and so on.
I've never done it but this answer looks like a good starting point. https://stackoverflow.com/a/11321499/385017
The method you'll want to use is vDSP_mmul for single precision or vDSP_mmulD for double precision. You might want to look at the documentation for it to get a better unerstanding of how to use it but heres an example to get you started.
float *matrixA; //set by you
float *matrixB; //set by you
float *matrixAB; //the matrix that the answer will be stored in
vDSP_mmul( matrixA, 1, matrixB, 1, matrixAB, 1, 4, 4, 4 );
// the 1s should be left alone in most situations
// The 4s in order are:
// the number of rows in matrix A
// the number of columns in matrix B
// the number of columns in matrix A and the number of rows in matrix B.

Related

Differentiation with Fourier Sine Transform (FFTW)

How can we calculate the first derivative of a function with FFTW_RODFT00 (sine transform)?
I have some luck calculating the 2nd derivative because sqrt(-1)^2 = -1 and whilst working with imaginary values, this returns an imaginary value that we can hand down the Inverse Sine Transform to get back df(x). With the first derivative, on the other hand, I fear we might have a real value (multiplication with i=sqrt(-1) ) to inverse transform with FFTW_REDFT00?
Here's my code:
#include "math.h"
#include "fftw3w.h"
#include "common.h"
//#include <complex>
#define M_PI 3.14159265358979323846
#define FFN_R2R 3
float b[FFN_R2R]={1,2,3};
static void normalize_r2r(void){
const float const_k=1.f/(2*(FFN_R2R+1));
for(unsigned int i=0; i<FFN_R2R; ++i) b[i]*=const_k;
}
static void delta_r2r(void) {
const float fM_PI= (float) M_PI;
fftwf_plan gplan[2];
gplan[0]= fftwf_plan_r2r_1d(FFN_R2R, b, b, FFTW_RODFT00, FFTW_ESTIMATE);
gplan[1]= fftwf_plan_r2r_1d(FFN_R2R, b, b, FFTW_RODFT00, FFTW_ESTIMATE);
fftwf_execute(gplan[0]);
/**/
const float rcpL=1.f/(1*(FFN_R2R+1));
unsigned int i;
float k;
for(i=0, k=0.f; i<FFN_R2R; ++i, ++k){
b[i]*= -( fM_PI*rcpL*(k+1.f) * fM_PI*rcpL*(k+1.f) );
}
fftwf_execute(gplan[1]);
normalize_r2r();
for(uint i=0; i<FFN_R2R; ++i) printf("%f \n", b[i]);
}

Using opencv's ptr pointer to manipulate pixels

I used opencv to read in a picture and split the image.
The divided matrix:
Odd rows are odd columns as A matrix;
The odd-numbered rows are evenly listed as B-matrices;
The odd-numbered rows are evenly listed as C-matrices;
Even-numbered even-numbered columns are D-matrices;
My code:
void SplitMat(Mat& src, Mat& objA, Mat& objB, Mat& objC, Mat& objD) {
// src: Input image, CV_16UC1
// objA: Output image, CV_16UC1
// objB: Output image, CV_16UC1
// objC: Output image, CV_16UC1
// objD: Output image, CV_16UC1
Mat dst;
src.copyTo(dst);
int row, col, i, j;
for (row = 0, i = 0; row < dst.rows - 1; row = row + 2, ++i) {
ushort* temp0 = dst.ptr<ushort>(row);
ushort* temp1 = dst.ptr<ushort>(row + 1);
ushort* obja = objA.ptr<ushort>(i);
ushort* objb = objB.ptr<ushort>(i);
ushort* objc = objC.ptr<ushort>(i);
ushort* objd = objD.ptr<ushort>(i);
for (col = 0, j = 0; col < dst.cols - 1; col = col + 2, ++j) {
obja[j] = temp0[col];
objb[j] = temp0[col + 1];
objc[j] = temp1[col];
objd[j] = temp1[col + 1];
}
}
}
Test result:
I don't know why the image in the x direction has become two images.
Please guide us. Is this a logical error or something else? thanks
I know the reason for this problem. Since the picture read is 8 bits, and the program uses 16 bits.
uchar -> ushort have a question.

How to implement Sobel operator

I have implemented Sobel operator in vertical direction. But the result which I am getting is very poor. I have attached my code below.
int mask_size= 3;
char mask [3][3]= {{-1,0,1},{-2,0,2},{-1,0,1}};
void sobel(Mat input_image)
{
/**Padding m-1 and n-1 zeroes to the result where m and n are mask_size**/
Mat result=Mat::zeros(input_image.rows+(mask_size - 1) * 2,input_image.cols+(mask_size - 1) * 2,CV_8UC1);
Mat result1=Mat::zeros(result.rows,result.cols,CV_8UC1);
int sum= 0;
/*For loop for copying original values to new padded image **/
for(int i=0;i<input_image.rows;i++)
for(int j=0;j<input_image.cols;j++)
result.at<uchar>(i+(mask_size-1),j+(mask_size-1))=input_image.at<uchar>(i,j);
GaussianBlur( result, result, Size(5,5), 0, 0, BORDER_DEFAULT );
/**For loop to implement the convolution **/
for(int i=0;i<result.rows-(mask_size - 1);i++)
for(int j=0;j<result.cols-(mask_size - 1);j++)
{
int counter=0;
int counterX=0,counterY=0;
sum= 0;
for(int k= i ; k < i + mask_size ; k++)
{
for(int l= j ; l< j + mask_size ; l++)
{
sum+=result.at<uchar>(k,l) * mask[counterX][counterY];
counterY++;
}
counterY=0;
counterX++;
}
result1.at<uchar>(i+mask_size/2,j+mask_size/2)=sum/(mask_size * mask_size);
}
/** Truncating all the extras rows and columns **/
result=Mat::zeros( result1.rows - (mask_size - 1) * 2, result1.cols - (mask_size - 1) * 2,CV_8UC1);
for(int i=0;i<result.rows;i++)
for(int j=0;j<result.cols;j++)
result.at<uchar>(i,j)=result1.at<uchar>(i+(mask_size - 1),j+(mask_size - 1));
imshow("Input",result);
imwrite("output2.tif",result);
}
My input to the algorithm is
My output is
I have also tried using Gaussian blur before actually convolving an image and the output I got is
The output which I am expecting is
The guide I am using is: https://www.tutorialspoint.com/dip/sobel_operator.htm
Your convolution looks ok although I only had a quick look.
Check your output type. It's unsigned char.
Now think about the values your output pixels may have if you have negative kernel values and if it is a good idea to store them in uchar directly.
If you store -1 in an unsigned char it will be wrapped around and your output is 255. In case you're wondering where all that excess white stuff is coming from. That's actually small negative gradients.
The desired result looks like the absolute of the Sobel output values.

How to merge a lot of square images via OpenCV?

How can I merge images like below into a single image using OpenCV (there can be any number of them both horizontally and vertically)? Is there any built-in solution to do it?
Additional pieces:
Well, it seems that I finished the puzzle:
Main steps:
Compare each pair of images (puzzle pieces) to know the relative position (findRelativePositions and getPosition).
Build a map knowing the relative positions of the pieces (buildPuzzle and builfForPiece)
Create the final collage putting each image at the correct position (final part of buildPuzzle).
Comparison between pieces A and B in step 1 is done checking for similarity (sum of absolute difference) among:
B is NORTH to A: A first row and B last row;
B is SOUTH to A: A last row and B first row;
B is WEST to A : A last column and B first column;
B is EAST to A : A first column and B last column.
Since images do not overlap, but we can assume that confining rows (columns) are quite similar, the key aspect is to use a (ad-hoc) threshold to discriminate between confining pieces or not. This is handled in function getPosition, with threshold parameter threshold.
Here the full code. Please let me know if something is not clear.
#include <opencv2\opencv.hpp>
#include <algorithm>
#include <set>
using namespace std;
using namespace cv;
enum Direction
{
NORTH = 0,
SOUTH,
WEST,
EAST
};
int getPosition(const Mat3b& A, const Mat3b& B, double& cost)
{
Mat hsvA, hsvB;
cvtColor(A, hsvA, COLOR_BGR2HSV);
cvtColor(B, hsvB, COLOR_BGR2HSV);
int threshold = 1000;
// Check NORTH
Mat3b AN = hsvA(Range(0, 1), Range::all());
Mat3b BS = hsvB(Range(B.rows - 1, B.rows), Range::all());
Mat3b AN_BS;
absdiff(AN, BS, AN_BS);
Scalar scoreN = sum(AN_BS);
// Check SOUTH
Mat3b AS = hsvA(Range(A.rows - 1, A.rows), Range::all());
Mat3b BN = hsvB(Range(0, 1), Range::all());
Mat3b AS_BN;
absdiff(AS, BN, AS_BN);
Scalar scoreS = sum(AS_BN);
// Check WEST
Mat3b AW = hsvA(Range::all(), Range(A.cols - 1, A.cols));
Mat3b BE = hsvB(Range::all(), Range(0, 1));
Mat3b AW_BE;
absdiff(AW, BE, AW_BE);
Scalar scoreW = sum(AW_BE);
// Check EAST
Mat3b AE = hsvA(Range::all(), Range(0, 1));
Mat3b BW = hsvB(Range::all(), Range(B.cols - 1, B.cols));
Mat3b AE_BW;
absdiff(AE, BW, AE_BW);
Scalar scoreE = sum(AE_BW);
vector<double> scores{ scoreN[0], scoreS[0], scoreW[0], scoreE[0] };
int idx_min = distance(scores.begin(), min_element(scores.begin(), scores.end()));
int direction = (scores[idx_min] < threshold) ? idx_min : -1;
cost = scores[idx_min];
return direction;
}
void resolveConflicts(Mat1i& positions, Mat1d& costs)
{
for (int c = 0; c < 4; ++c)
{
// Search for duplicate pieces in each column
set<int> pieces;
set<int> dups;
for (int r = 0; r < positions.rows; ++r)
{
int label = positions(r, c);
if (label >= 0)
{
if (pieces.count(label) == 1)
{
dups.insert(label);
}
else
{
pieces.insert(label);
}
}
}
if (dups.size() > 0)
{
int min_idx = -1;
for (int duplicate : dups)
{
// Find minimum cost position
Mat1d column = costs.col(c);
min_idx = distance(column.begin(), min_element(column.begin(), column.end()));
// Keep only minimum cost position
for (int ir = 0; ir < positions.rows; ++ir)
{
int label = positions(ir, c);
if ((label == duplicate) && (ir != min_idx))
{
positions(ir, c) = -1;
}
}
}
}
}
}
void findRelativePositions(const vector<Mat3b>& pieces, Mat1i& positions)
{
positions = Mat1i(pieces.size(), 4, -1);
Mat1d costs(pieces.size(), 4, DBL_MAX);
for (int i = 0; i < pieces.size(); ++i)
{
for (int j = i + 1; j < pieces.size(); ++j)
{
double cost;
int pos = getPosition(pieces[i], pieces[j], cost);
if (pos >= 0)
{
if (costs(i, pos) > cost)
{
positions(i, pos) = j;
costs(i, pos) = cost;
switch (pos)
{
case NORTH:
positions(j, SOUTH) = i;
costs(j, SOUTH) = cost;
break;
case SOUTH:
positions(j, NORTH) = i;
costs(j, NORTH) = cost;
break;
case WEST:
positions(j, EAST) = i;
costs(j, EAST) = cost;
break;
case EAST:
positions(j, WEST) = i;
costs(j, WEST) = cost;
break;
}
}
}
}
}
resolveConflicts(positions, costs);
}
void builfForPiece(int idx_piece, set<int>& posed, Mat1i& labels, const Mat1i& positions)
{
Point pos(-1, -1);
// Find idx_piece on grid;
for (int r = 0; r < labels.rows; ++r)
{
for (int c = 0; c < labels.cols; ++c)
{
if (labels(r, c) == idx_piece)
{
pos = Point(c, r);
break;
}
}
if (pos.x >= 0) break;
}
if (pos.x < 0) return;
// Put connected pieces
for (int c = 0; c < 4; ++c)
{
int next = positions(idx_piece, c);
if (next > 0)
{
switch (c)
{
case NORTH:
labels(Point(pos.x, pos.y - 1)) = next;
posed.insert(next);
break;
case SOUTH:
labels(Point(pos.x, pos.y + 1)) = next;
posed.insert(next);
break;
case WEST:
labels(Point(pos.x + 1, pos.y)) = next;
posed.insert(next);
break;
case EAST:
labels(Point(pos.x - 1, pos.y)) = next;
posed.insert(next);
break;
}
}
}
}
Mat3b buildPuzzle(const vector<Mat3b>& pieces, const Mat1i& positions, Size sz)
{
int n_pieces = pieces.size();
set<int> posed;
set<int> todo;
for (int i = 0; i < n_pieces; ++i) todo.insert(i);
Mat1i labels(n_pieces * 2 + 1, n_pieces * 2 + 1, -1);
// Place first element in the center
todo.erase(0);
labels(Point(n_pieces, n_pieces)) = 0;
posed.insert(0);
builfForPiece(0, posed, labels, positions);
// Build puzzle starting from the already placed elements
while (todo.size() > 0)
{
auto it = todo.begin();
int next = -1;
do
{
next = *it;
++it;
} while (posed.count(next) == 0 && it != todo.end());
todo.erase(next);
builfForPiece(next, posed, labels, positions);
}
// Posed all pieces, now collage!
vector<Point> pieces_position;
Mat1b mask = labels >= 0;
findNonZero(mask, pieces_position);
Rect roi = boundingRect(pieces_position);
Mat1i lbls = labels(roi);
Mat3b collage(roi.height * sz.height, roi.width * sz.width, Vec3b(0, 0, 0));
for (int r = 0; r < lbls.rows; ++r)
{
for (int c = 0; c < lbls.cols; ++c)
{
if (lbls(r, c) >= 0)
{
Rect rect(c*sz.width, r*sz.height, sz.width, sz.height);
pieces[lbls(r, c)].copyTo(collage(rect));
}
}
}
return collage;
}
int main()
{
// Load images
vector<String> filenames;
glob("D:\\SO\\img\\puzzle*", filenames);
vector<Mat3b> pieces(filenames.size());
for (int i = 0; i < filenames.size(); ++i)
{
pieces[i] = imread(filenames[i], IMREAD_COLOR);
}
// Find Relative positions
Mat1i positions;
findRelativePositions(pieces, positions);
// Build the puzzle
Mat3b puzzle = buildPuzzle(pieces, positions, pieces[0].size());
imshow("Puzzle", puzzle);
waitKey();
return 0;
}
NOTE
No, there is no built-in solution to perform this. Image stitching won't work since the images are not overlapped.
I cannot guarantee that this works for every puzzle, but should work for the most.
I probably should have worked this couple of hours, but it was fun :D
EDIT
Adding more puzzle pieces generates wrong results in the previous code version. This was due the (wrong) assumption that at most one piece is good enough to be connected with a given piece.
Now I added a cost matrix, and only the minimum cost piece is saved as neighbor of a given piece.
I added also a resolveConflicts function that avoid that one piece can be merged (in non-conflicting position) with more than one piece.
This is the result adding more pieces:
UPDATE
Considerations after increasing the number of puzzle pieces:
This solution it's dependent on the input order of pieces, since it turns out it has a greedy approach to find neighbors.
While searching for neighbors, it's better to compare the H channel in the HSV space. I updated the code above with this improvement.
The final solution needs probably some kind of global minimization of the of a global cost matrix. This will make the method independent on the input order. I'll be back on this asap.
Once you have loaded this images as OpenCV Mat, you can concatenate these Mat both vertically or horizontally using:
Mat A, B; // Images that will be concatenated
Mat H; // Here we will concatenate A and B horizontally
Mat V; // Here we will concatenate A and B vertically
hconcat(A, B, H);
vconcat(A, B, V);
If you need to concatenate more than two images, you can use these methods recursively.
By the way, I think these methods are not included in the OpenCV documentation, but I have used them in the past.

HDF5 rowmajor or colmajor

Is it possible to know if a matrix stored in HDF5 format is in RowMajor or ColMajor? For example when I save matrices from octave, which stores them internally as ColMajor, I need to transpose them when I read them in my C code where matrices are stored in RowMajor, and vice versa.
HDF5 stores data in row major order:
HDF5 uses C storage conventions, assuming that the last listed dimension is the fastest-changing dimension and the first-listed dimension is the slowest changing.
from the HDF5 User's Guide.
However, if you're using Octave's built-in HDF5 interface, it will automatically transpose the arrays for you. In general, how the data is actually written in the HDF5 file should be completely opaque to the end-user, and the interface should deal with differences in array ordering, etc.
As #Yossarian pointed out. HDF5 always stores data as row-major (C convention). Octave is the same as Fortran and internally stores data as column-major.
When writing a matrix from Octave, the HDF5 layer does the transpose for you, so it is always written as row-major no matter what language you use. This provides the portability of the file.
There is a very good example in the HDF5 User's Guide section 7.3.2.5, as mentioned by #Yossarian. Here's the example (almost) reproduced using Octave:
octave:1> A = [ 1:3; 4:6 ]
A =
1 2 3
4 5 6
octave:2> save("-hdf5", "test.h5", "A")
octave:3> quit
~$ h5dump test.h5
HDF5 "test.h5" {
GROUP "/" {
COMMENT "# Created by Octave 3.6.4, Fri Jun 13 08:36:16 2014 MDT <user#localhost>"
GROUP "A" {
ATTRIBUTE "OCTAVE_NEW_FORMAT" {
DATATYPE H5T_STD_U8LE
DATASPACE SCALAR
DATA {
(0): 1
}
}
DATASET "type" {
DATATYPE H5T_STRING {
STRSIZE 7;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SCALAR
DATA {
(0): "matrix"
}
}
DATASET "value" {
DATATYPE H5T_IEEE_F64LE
DATASPACE SIMPLE { ( 3, 2 ) / ( 3, 2 ) }
DATA {
(0,0): 1, 4,
(1,0): 2, 5,
(2,0): 3, 6
}
}
}
}
}
Notice how the HDF5 layer has transposed the matrix to make sure it is stored in row-major format.
Then an example of reading it in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hdf5.h>
#define FILE "test.h5"
#define DS "A/value"
int
main(int argc, char **argv)
{
int i = 0;
int j = 0;
int n = 0;
int x = 0;
int rank = 0;
hid_t file_id;
hid_t space_id;
hid_t dset_id;
herr_t stat;
hsize_t *dims = NULL;
int *data = NULL;
file_id = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
dset_id = H5Dopen(file_id, DS, dset_id);
space_id = H5Dget_space(dset_id);
n = H5Sget_simple_extent_npoints(space_id);
rank = H5Sget_simple_extent_ndims(space_id);
dims = malloc(rank*sizeof(int));
stat = H5Sget_simple_extent_dims(space_id, dims, NULL);
printf("rank: %d\t dimensions: ", rank);
for (i = 0; i < rank; ++i) {
if (i == 0) {
printf("(");
}
printf("%llu", dims[i]);
if (i == (rank -1)) {
printf(")\n");
} else {
printf(" x ");
}
}
data = malloc(n*sizeof(int));
memset(data, 0, n*sizeof(int));
stat = H5Dread(dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
data);
printf("%s:\n", DS);
for (i = 0; i < dims[0]; ++i) {
printf(" [ ");
for (j = 0; j < dims[1]; ++j) {
x = i * dims[1] + j;
printf("%d ", data[x]);
}
printf("]\n");
}
stat = H5Sclose(space_id);
stat = H5Dclose(dset_id);
stat = H5Fclose(file_id);
return(EXIT_SUCCESS);
}
When compiled and run gives:
~$ h5cc -o rmat rmat.c
~$ ./rmat
rank: 2 dimensions: (3 x 2)
A/value:
[ 1 4 ]
[ 2 5 ]
[ 3 6 ]
This is great as it means the matrices are stored optimized in memory. What it does mean though is that you have to change how you do your calculations. For row-major you need to do pre-multiplication, while for column-major you should be doing post-multiplication.
Here is an example, hopefully it is explained a bit clearer.
Does this help?

Resources