OpenCV image pointer not working as expected - image

So I have to do some operations on part of an image. The operation is not relevant (i dont change this code at all), but the way i create the pointer changes the results i get. I dont understand why that happens.
Why does this code gets the result I want:
for(int row = 0; row < 70; ++row) {
for(int col = 48; col < 208; ++col) {
uchar* p = c.ptr(row+col);
*p = (1-circuloBinario.at<unsigned char>(row,col-48))*(*p) + circuloBinario.at<unsigned char>(row,col-48)*limite;
}
}
And this one doesnt?
for(int row = 0; row < 70; ++row) {
uchar* p = c.ptr(row+48);
for(int col = 48; col < 208; ++col) {
*p = (1-circuloBinario.at<unsigned char>(row,col-48))*(*p) + circuloBinario.at<unsigned char>(row,col-48)*limite;
p++;
}
}
By the way, I dont get any errors with the second code, the problem is that the result I get is not what I expect (it starts modifying the image from a row bigger than 0 and starts from column 0 instead of 48).
Thanks.

Mat::ptr returns a pointer to the specified matrix row.
See the documentation here: http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-ptr
So neither c.ptr(row+col) nor c.ptr(row+48) make sense because you should only be passing a row index to the ptr function.

The way you use Mat::pt is apparently incorrect, as mentioned by M456.
If you want to modify the value of some elements of the matrix, why don't you use the following syntax?
c.at<element_type>(row, col) = new_value;

Related

How to access intensity of all the pixels of Image in openCV C++

For accessing single point, I am using this line of code and it works
int intensity = gray_image.at<uchar>(Point(100, 100));
However when I use this code to access all the pixels in image, it gives memory error,
for (int i = 0; i < gray_image.rows;i++)
{
for (int j = 0; j < gray_image.cols; j++) {
intensity += gray_image.at<uchar>(Point(i, j));
}
}
When I run above code, it does not give compile time error but gives memory exception. Where am I going wrong?
You can just skip the use of Point and do the following.
for (int i = 0; i < gray_image.rows;i++)
{
for (int j = 0; j < gray_image.cols; j++) {
intensity += gray_image.at<uchar>(i, j);
}
}
You're requesting a pixel (j,i) that doesn't exist. This wouldn't have been an error in a square image (where the number of rows = number of columns), but you're using a rectangular image.
The Mat::at function has multiple prototypes, the two that you're concerned with are:
C++: template<typename T> T& Mat::at(int i, int j)
C++: template<typename T> T& Mat::at(Point pt)
The documentation for Mat::at states that Point pt is defined as the Element position specified as Point(j,i), so you've effectively swapped your rows and columns.
The reason this happens is because the image is stored in a 1D array of pixels, and to get a pixel Point (r,c) is translated to p = r * image.cols + c;

handsontable's subproject rulejs: how to get the calculated value of a formula cell?

rulejs subproject of handsontable is great. But I can't find anything that emulates the "paste special" of spreadsheets. So, if I write
afterOnCellMouseDown: function(r, c) {
var x = hotdata[c['row']][c['col']];
document.getElementById("valorCelCorrente").innerHTML = x;}
I get the formula, not the calculated value that the cell is displaying. Same happens with a manual copy & paste.
A fiddle with example: https://jsfiddle.net/zota/j2a04w83/3/
Any clue? Thanks a lot
Julio
I had the same issue, but solved it with the help of this question.
Firstly you can use hot.plugin.helper.cellValue('CELL REF') to get the actual value, but when using getData() you're looking at the whole set. To solve this problem I basically iterated through the array replacing any formula cells with the value (I only needed columns up until 'K'):
var aCols = ['A','B','C','D','E','F','G','H','I','J','K'];
var data = hot.getData();
for (i = 0; i < data.length; i++){
for(j = 0; j < data[i].length; j++){
if(data[i][j].toString().indexOf('=') > -1){
data[i][j] = hot.plugin.helper.cellValue (aCols[j] + (i+1));
}
}
}

Using a for loop to create a pattern output and how to change the formula to change the pattern

I have to work out how to change code so that the output in the columns or rows I have changes. I have done the following for one that is supposed to print 5 by 5 rows of asterisks with the third row having plus signs instead. It is almost right but something is making the plus signs happen on the 4th row instead of the third. My lecturer said something about it in class being to do with when the value is grabbed, but I can't remember what and it's the weekend so I can't ask him.
I have read a few questions and that gave me a jumping point but I'm still unclear on the following things;
Can you please tell me where I'm going wrong with the third line and fourth line being opposite to how I would like them and give me any pointers on how to change things in specific rows or columns?
Thanks, here is my code;
public class OutputB
{
public static void main(String[] args)
{
int rows = 5; // tells the program how many rows
int cols = 5; // tells the program how many columns
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
if(r != 3)
{
System.out.print("*");
}
else
{
System.out.print("+");
}
System.out.println();
You're starting at zero, which means the third increment is actually the fourth. You have two options to fix this. You can either subtract 1 from the desired row number...
int rows = 5; // tells the program how many rows
int cols = 5; // tells the program how many columns
int rowOfThePlusSymbol = 3;
...
if(r != rowOfThePlusSymbol-1)
...or you you can start at 1 in your for loop. If you do so, be sure to put <= in your for loop also, so you loop the desired number of times.
...
for (int r = 1; r <= rows; r++)
{
for (int c = 1; c <= cols; c++)
...
}
I don't encourage using the latter solution because it is normal practice to start at zero when using increments or indexes of lists.

Delay code execution in VCL Forms Application

Need to animate a sorting algorithm, with source code line by line visualization.
INTRO:
For the begining, there is a FORM (see it in the picture attached). On top of that form is displayed a dinamicaly created array of Edit components, containing the array to sort.
A little below, on the right is placed a Memo component, containing the algorithm. At the left of each line of that algorithm, dinamicaly is placed a Label, that indicates the line number in algorithm.
The idea is to highlight line by colouring that label, where is the execution at the moment. Sorting starts when "Start" button is clicked. The action for it is following:
int n = 10;
bool swapped = true; hl(1);
int j = 0; hl(2);
int tmp; hl(3);
while (swapped) { hl(4);
swapped = false; hl(5);
j++; hl(6);
for (int i = 0; i < n - j; i++) { hl(7);
if (arr[i] > arr[i + 1]) { hl(8);
tmp = arr[i]; hl(9);
arr[i] = arr[i + 1]; hl(10);
arr[i + 1] = tmp; hl(11);
swapped = true; hl(12);
} hl(13);
} hl(14);
} hl(15);
The hl function must colour labels and pause execution by using Sleep() function
void TForm2::hl(int l)
{
for (int i = 0; i < 24; i++) {
Form2->lines[i]->Font->Color = clGray;
}
Form2->lines[l-1]->Font->Color = clRed;
Sleep(300);
}
PROBLEM:
Code execution is pausing (sleep function works properly), but the labels are still gray, with no visible changes, except the last one, when event finishes. The 15th line is red.
QUESTION:
Can anybody tell me, where I'm wrong, and how to do it right?
http://i.stack.imgur.com/crGyC.jpg
You need to allow the paint message to be processed in order to visually update the display. You can do that with either the Refresh or Update procedures:
Form2->Lines[l-1]->Font->Color = clGray;
Form2->Update(); // or Form2->Refresh();

selenium xpath query

var value = selenium.GetXpathCount("//div[contains(#id,'spnPriceDetails')]");
int clickNo = 1;
for (var j = 1; j <= value; j++)
{
if (clickNo == j)
{
selenium.Click("//div[contains(#id,'spnPriceDetails')]");
}
clickNo = clickNo + 1;
}
I have 25 same links on one page I have Identify total number of links using Xpath Count. I Click on First Link But when i try to click on second and thirld link it click on first link every time instead of second and thirld
Your code isn't specifying a later link, it's doing the same search each time, retrieving all 25 that match "//div[contains(#id,'spnPriceDetails')]" and then clicking the first one in the resulting set of matches. You need to add the iterator variable into the search string, like so:
for (var j = 1; j <= value; j++)
{
selenium.Click("(//div[contains(#id,'spnPriceDetails')])[" + j + "]");
}
This way it will click through each value in the list of matches.
Note: I can't remember if xpath will start with 0 index or 1-index. It's supposed to be 1, but if it isn't you may need to start your loop at 0 instead.
I would try to use Position in the xpath expression.
I have created a fluent Xpath library that might be helpful:
The code would be something like:
for (int i = 1; i <= value; i++)
{
selenium.Click(XPathFinder.Find.Tag("div").With.Attribute("id").
Containing("spnPriceDetails").And.Position(i).ToXPathExpression());
}
Some more info on the XPath library can be found here:
http://www.unit-testing.net/CurrentArticle/How-To-Write-XPath-for-Selenium-Tests.html
I guess the plain xpath string way would be:
for (int i = 1; i <= value; i++)
{
selenium.Click(string.Format("//div[contains(#id,'spnPriceDetails') and position()={0}]",i)); }

Resources