How do I Double.parseDouble / Float.parseFloat in J++ - j++

Microsoft J++ does not have a Double.parseDouble() method. How do I do this?

String val = "12.45";
double d = new Double(val).doubleValue();
Float f = new Float(val).floatValue();

Related

How to transform DCT block back to color values?

So I am trying to use this
https://www.nayuki.io/res/fast-discrete-cosine-transform-algorithms/NaiveDct.cs
in my image processing assignment, in which we are supposed to apply DCT on an a picture. (8x8 block)
static public double[,] Dct(double[,] array)
{
double[,] dct = new double[N, M];
double factor = Math.PI / (N * M);
//dct
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
double sum = 0;
for (int k = 0; k < M; k++)
{
for (int l = 0; l < N; l++)
{
sum += array[k, l] * Math.Cos((k * M + l + 0.5) * (i * M + j) * factor);
}
}
dct[i, j] = (int) sum;
}
}
return dct;
}
This is the method I use for the forward transformation.
Results on an 8x8 block look like this
But trying to restore the "color block" (in this case it was used on the "blue" values of an rgb color).
With this code:
static public double[,] inverseDct(double[,] array)
{
double[,] colorBlock = new double[N, M];
double factor = Math.PI / (N * M);
//dct
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
double sum = array[0, 0] / 2;
for (int k = 0; k < M; k++)
{
int l = 0;
if (k == 0) l = 1;
for (; l < N; l++)
{
sum += array[k, l] * Math.Cos((k * M + l) * ((i * M + j) + 0.5) * factor);
}
}
colorBlock[i, j] = (int) sum;
}
}
return colorBlock;
}
It doesn't seem to work properly because the output of (the blue color value block in the picture above) now says this:
I posted my entire code here:
https://pastebin.com/XbCE3kBK
How do I get back my "color" values from the DCT Block? (doing the reverse transformation?)

Draw a sphere using sectors and stack WebGL

I'm trying to draw a sphere using sectors and stacks algorithm but it output nothing and do not know where is the problem. Any help?
I implemented the algorithm literally as written in: http://www.songho.ca/opengl/gl_sphere.html
Everything is working fine except the coloredShpere function
this is a photo of what appears to me when I run this function:
and you can find the whole code in: https://drive.google.com/open?id=1dnnkk1w7oq4O7hPTMeGRkyELwi4tcl5X
let mesh = createMesh(gl);
const PI = 3.1415926;
const r = 1.0;
const stackCount = 16;
const sectorCount = 16;
let x : number;
let y : number;
let z : number;
let xy : number;
let vertices: number[] = new Array();
let normals : number[] = new Array();
let texCoords : number[] = new Array();
let nx: number;
let ny: number;
let nz: number;
let lengthInv: number;
lengthInv = 1.0 / r;
let s: number;
let t: number;
let sectorStep = 2 * PI / sectorCount;
let stackStep = PI / stackCount;
let sectorAngle : number;
let stackAngle : number;
for(let i = 0; i<=stackCount; i++) {
stackAngle = PI/2 - i*stackStep; //-90 to +90
xy = r*Math.cos(stackAngle);
z = r*Math.sin(stackAngle);
for(let j = 0; j<=sectorCount; j++) {
sectorAngle = j*sectorAngle; //0 to 360
x = xy*Math.cos(sectorAngle);
y = xy*Math.sin(sectorAngle);
vertices.push(x);
vertices.push(y);
vertices.push(z);
nx = x * lengthInv;
ny = y * lengthInv;
nz = z * lengthInv;
normals.push(nx);
normals.push(ny);
normals.push(nz);
// vertex tex coord (s, t) range between [0, 1]
s = j / sectorCount;
t = i / stackCount;
texCoords.push(s);
texCoords.push(t);
}
}
// generate CCW index list of sphere triangles
// indices
// k1--k1+1
// | / |
// | / |
// k2--k2+1
let indices: number[] = new Array();
let k1 : number;
let k2 : number;
for(let i = 0; i<stackCount; i++) {
k1 = i * (sectorCount + 1); //frist stack
k2 = k1 + sectorCount + 1; //second stack
for(let j = 0; j<sectorCount; j++) {
//k1, k2, k1+1
if(i != 0) {
indices.push(k1);
indices.push(k2);
indices.push(k1+1);
}
//k1+1, k2, k2+1
if(i != (stackCount-1)) {
indices.push(k1+1);
indices.push(k2);
indices.push(k2+1);
}
}
}
mesh.setBufferData("positions", new Float32Array(vertices), gl.STATIC_DRAW);
//mesh.setBufferData("colors", new Uint8Array(), gl.STATIC_DRAW);
mesh.setElementsData(new Uint32Array(indices), gl.STATIC_DRAW);
//mesh.setBufferData("colors", new Uint8Array(), gl.STATIC_DRAW);
return mesh;
Suggestion: Learn how to use console.log and your browser's debugger
I didn't check if your code actually works or not but I did add these lines at the bottom of what you posted above
console.log(vertices);
console.log(indices);
and what I saw was
All those NaN values are clearly wrong
Stepping through the code comes to this line
sectorAngle = j*sectorAngle; //0 to 360
which is where the NaN is generated
which doesn't match the article you linked to
sectorAngle = j * sectorStep; // starting from 0 to 2pi
Whether or not that's the only issue I don't know but if there are more then use console.log and the debugger to help find the issue. One way to make the code easier to debug is set stackCount and sectorCount to something small like 4 and 2 respectively and then you should have some idea what all the values should be and you can compare with what values you are getting.
If someone interested to know the solution, this is the code after some improvements:
`
let mesh = createMesh(gl);
const PI = 3.1415926;
const r = 1.0;
let vertices = [];
let colors = [];
for(let i = 0; i<=verticalResolution; ++i) {
let theta = i * Math.PI / verticalResolution; //-90 to 90
let sinTheta = Math.sin(theta);
let cosTheta = Math.cos(theta);
for(let j = 0; j<=horizontalResolution; ++j) {
let phi = j * 2 * Math.PI / horizontalResolution; //0 to 360
let sinPhi = Math.sin(phi);
let cosPhi = Math.cos(phi);
let x = sinTheta*cosPhi;
let y = cosTheta;
let z = sinTheta*sinPhi;
vertices.push(r*x);
vertices.push(r*y);
vertices.push(r*z);
colors.push((x+1)/2*255);
colors.push((y+1)/2*255);
colors.push((z+1)/2*255);
colors.push(255);
}
}
// generate CCW index list of sphere triangles
// indices
// k1--k1+1
// | / |
// | / |
// k2--k2+1
let indices = [];
for(let i = 0; i<verticalResolution; ++i) {
for(let j = 0; j<horizontalResolution; ++j) {
let first = (i * (horizontalResolution + 1)) + j;
let second = first + horizontalResolution + 1;
indices.push(first);
indices.push(second);
indices.push(first+1);
indices.push(second);
indices.push(second+1);
indices.push(first+1);
}
}
mesh.setBufferData("positions", new Float32Array(vertices), gl.STATIC_DRAW);
mesh.setBufferData("colors", new Uint8Array(colors), gl.STATIC_DRAW);
mesh.setElementsData(new Uint32Array(indices), gl.STATIC_DRAW);
return mesh;`
Output of the code

multi-level (4) Otsu thresholding

I'm trying to implement multi-level Otsu's thresholding, more specifically I need 3 thresholds/4 classes.
I'm aware of 2 similair questions on SO about it: #34856019 and #22706742.
The problem is that I don't get good results: I've read several articles with sample images and thresholds found by that code differ from the ones in these papers.
Let's say I have a picture with 3 circles on the black background, the brightness of the circles differ from very bright to dark:
Sample Image
Am I right to suppose to get as a result 4 classes: black background and 3 more classes according to circles' intensity?
My program gives me these threshold values: 226, 178, 68
As a result, the third circle is completely invisible - it's in the same class as the background.
Can someone please check these values and/or the source code? Maybe it is possible to check this image using Matlab or somehow else...
By the way, what's the best way to handle divisions by zero, which happen often with zero values in histogram?
The source code:
void MultilevelThresholding(cv::Mat& src)
{
int histogram[256] = { 0 };
int pixelsCount = src.cols * src.rows;
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
uchar value = src.at<uchar>(y, x);
histogram[value]++;
}
}
double c = 0;
double Mt = 0;
double p[256] = { 0 };
for (int i = 0; i < 256; i++)
{
p[i] = (double) histogram[i] / (double) pixelsCount;
Mt += i * p[i];
}
int optimalTreshold1 = 0;
int optimalTreshold2 = 0;
int optimalTreshold3 = 0;
double maxBetweenVar = 0;
double w0 = 0;
double m0 = 0;
double c0 = 0;
double p0 = 0;
double w1 = 0;
double m1 = 0;
double c1 = 0;
double p1 = 0;
double w2 = 0;
double m2 = 0;
double c2 = 0;
double p2 = 0;
for (int tr1 = 0; tr1 < 256; tr1++)
{
p0 += p[tr1];
w0 += (tr1 * p[tr1]);
if (p0 != 0)
{
m0 = w0 / p0;
}
c0 = p0 * (m0 - Mt) * (m0 - Mt);
c1 = 0;
w1 = 0;
m1 = 0;
p1 = 0;
for (int tr2 = tr1 + 1; tr2 < 256; tr2++)
{
p1 += p[tr2];
w1 += (tr2 * p[tr2]);
if (p1 != 0)
{
m1 = w1 / p1;
}
c1 = p1 * (m1 - Mt) * (m1 - Mt);
c2 = 0;
w2 = 0;
m2 = 0;
p2 = 0;
for (int tr3 = tr2 + 1; tr3 < 256; tr3++)
{
p2 += p[tr3];
w2 += (tr3 * p[tr3]);
if (p2 != 0)
{
m2 = w2 / p2;
}
c2 = p2 * (m2 - Mt) * (m2 - Mt);
c = c0 + c1 + c2;
if (maxBetweenVar < c)
{
maxBetweenVar = c;
optimalTreshold1 = tr1;
optimalTreshold2 = tr2;
optimalTreshold3 = tr3;
}
}
}
}
So, I've figured it out. The final source code for 4 classes (3 thresholds) Otsu thresholding:
// cv::Mat& src - source image's matrix
int histogram[256] = { 0 };
int pixelsCount = src.cols * src.rows;
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
uchar value = src.at<uchar>(y, x);
histogram[value]++;
}
}
double c = 0;
double Mt = 0;
double p[256] = { 0 };
for (int i = 0; i < 256; i++)
{
p[i] = (double) histogram[i] / (double) pixelsCount;
Mt += i * p[i];
}
int optimalTreshold1 = 0;
int optimalTreshold2 = 0;
int optimalTreshold3 = 0;
double maxBetweenVar = 0;
double w0 = 0;
double m0 = 0;
double c0 = 0;
double p0 = 0;
double w1 = 0;
double m1 = 0;
double c1 = 0;
double p1 = 0;
double w2 = 0;
double m2 = 0;
double c2 = 0;
double p2 = 0;
for (int tr1 = 0; tr1 < 256; tr1++)
{
p0 += p[tr1];
w0 += (tr1 * p[tr1]);
if (p0 != 0)
{
m0 = w0 / p0;
}
c0 = p0 * (m0 - Mt) * (m0 - Mt);
c1 = 0;
w1 = 0;
m1 = 0;
p1 = 0;
for (int tr2 = tr1 + 1; tr2 < 256; tr2++)
{
p1 += p[tr2];
w1 += (tr2 * p[tr2]);
if (p1 != 0)
{
m1 = w1 / p1;
}
c1 = p1 * (m1 - Mt) * (m1 - Mt);
c2 = 0;
w2 = 0;
m2 = 0;
p2 = 0;
for (int tr3 = tr2 + 1; tr3 < 256; tr3++)
{
p2 += p[tr3];
w2 += (tr3 * p[tr3]);
if (p2 != 0)
{
m2 = w2 / p2;
}
c2 = p2 * (m2 - Mt) * (m2 - Mt);
double p3 = 1 - (p0 + p1 + p2);
double w3 = Mt - (w0 + w1 + w2);
double m3 = w3 / p3;
double c3 = p3 * (m3 - Mt) * (m3 - Mt);
double c = c0 + c1 + c2 + c3;
if (maxBetweenVar < c)
{
maxBetweenVar = c;
optimalTreshold1 = tr1;
optimalTreshold2 = tr2;
optimalTreshold3 = tr3;
}
}
}
}
Source image
Result: 3 thresholds / 4 classes
threshold values: 179, 92, 25

Obtain array from IplImage in JavaCV

I need to convert the code below from C++ to Java. In C++ I use openCV and I need to convert it in Java using JavaCV.
IplImage* img = cvLoadImage(argv[0]);
int rows = img->height;
int cols = img->width;
Mat matimg(img);
vector<vector<double> > img_vec(rows, vector<double>(cols));
for (int i=0; i < rows; i++) {
for (int j =0; j < cols; j++){
unsigned char temp;
temp = ((uchar*) matimg.data + i * matimg.step)[j * matimg.elemSize() + 1 ];
img_vec[i][j] = (double) temp;
}
}
I've tried the following conversion to java, but it doesn't work properly. I printed the values of temp and it is 0 all the times and for the same imgage the values of matimg.step and matimg.elemSize() are different in the C++ code and the Java code.
In c++ I get matimg.step = 2400 and matimg.elemSize() = 3 while in Java i get 3000 and 1.
Here is the code in java:
IplImage img = cvLoadImage(argv[0]);
int rows = img.height();
int cols = img.width();
CvMat matimg = img.asCvMat();
double img_vec[][] = new double[rows][cols];
for (int i=0; i < rows; i++) {
for (int j =0; j < cols; j++){
short temp;
temp = matimg.data_s().get(i * matimg.step() + j * matimg.elemSize() + 1);
img_vec[i][j] = (double) temp;
}
}
I don't understand where am I doing wrong?
Any help is appreciated,
Thanks.
I've solved my problem using this:
ByteBuffer buffer = img.getByteBuffer();
double img_vec[][] = new double[rows][cols];
for (int i=0; i < rows; i++) {
for (int j =0; j < cols; j++){
int ind = i * img.widthStep() + j * img.nChannels() + 1;
img_vec[i][j] = (buffer.get(ind) & 0xFF);
}
}

text parsing to XML using JDOM

I an newbee need you your help.
I am converting a text in to XML (with some extra attributes)
my text is
SPLINE(N, -1.151,1.002, -0.161,0.997, 0.840,-0.004, OUTLINED)
i need to have in XML using JDOM like following
<SPLINE n="3" x1="0.840" y1="-1.004" x2 ="-0.161" y2 ="0.997" x3 ="0.840" y3"-0.004" prim_style="OUTLINED" />
I can convert simply if N is fixed , in the above example N= 3 , so therefore has 3 x and 3 y coordinates. But If I am using for loop as below the result is not as excepted . any help will be great
root.addContent(child);
document.setContent(root);
int i = Integer.parseInt(temp_token[2]);
int count = i * 2 + i + 4;
for (int j = 0; j <= count - 5; j = j + 3) {
String x = null;
String y = null;
Element SPLINE = new Element("SPLINE")
.setAttribute("n", temp_token[2])
.setAttribute("x", temp_token[j + 4])
.setAttribute("y", temp_token[j + 5])
.setAttribute("prim_style",
temp_token[count]);
child.addContent(SPLINE);
From the above code
You should not be creating the SPLINE element inside the loop, it should be outside... (and variable names should be lowercase)
root.addContent(child);
document.setContent(root);
int i = Integer.parseInt(temp_token[2]);
int count = i * 2 + i + 4;
Element spline = new Element("SPLINE");
child.addContent(SPLINE);
spline.setAttribute("n", temp_token[2]);
int pair = 0;
for (int j = 0; j <= count - 5; j = j + 3) {
pair++;
spline.setAttribute("x" + pair, temp_token[j + 4]);
spline.setAttribute("y" + pair, temp_token[j + 5]);
}
spline.setAttribute("prim_style",temp_token[count]);

Resources