Does anyone know how to obtain the pixel value for each channel (RGB) from 16-bit tiff images using the magick package in R? Currently I am using Mathematica to perform this operation, because I could not find an equivalent way to doing it in mathematica.
I have tried to read the pixel value from the image-magick package and the results is a raw type (e.g. "ff"). I used the function rawToNum (package "pack") to convert the raw type to numeric and the results is close to what I obtain using ImageDate function in Mathematica, but not exactly the same.
You can also access the pixels as a numeric array with the magick package. The example is based on this vignette from the package.
library(magick)
tiger <- image_read('http://jeroen.github.io/images/tiger.svg')
tiger_tiff <- image_convert(tiger, "tiff")
# Access data in raw format and convert to integer
tiger_array <- as.integer(tiger_tiff[[1]])
Then if check the dimension and type you get:
dim(tiger_array)
[1] 900 900 4
is.numeric(tiger_array)
[1] TRUE
I don't know too much about R at all, but I guess you can "shell out" and execute an external command, using system() or somesuch.
If, so, maybe you can use this. First, let's make a 16-bit TIFF file that is a gradient from red-blue just 10 pixels wide and 1 pixel tall:
convert -size 10x1 gradient:red-blue image.tiff
Now we can dump the pixels to a file using ImageMagick:
convert image.tiff rgb:image.rgb
# Now check its length - yes, 60 bytes = 10 pixels with 2 bytes each for RG &B
ls -l image.rgb
-rw-r--r-- 1 mark staff 60 11 Jul 10:32 image.rgb
We can also write the data to stdout like this:
convert image.tiff rgb:-
and also look at it with 1 pixel per line (6 bytes)
convert image.tiff rgb:- | xxd -g 3 -c 6
00000000: ffff00 000000 ...... # Full Red, no Green, no Blue
00000006: 8de300 00721c ....r. # Lots of Red, no Green, a little Blue
0000000c: 1cc700 00e338 .....8
00000012: aaaa00 005555 ....UU
00000018: 388e00 00c771 8....q
0000001e: c77100 00388e .q..8.
00000024: 555500 00aaaa UU....
0000002a: e33800 001cc7 .8....
00000030: 721c00 008de3 r.....
00000036: 000000 00ffff ...... # No Red, no Green, full Blue
I'm hoping you can do something like that in R, with:
system("convert image.tif rgb:-")
Another way of dumping the pixels might be with Perl to slurp the entire file and then unpack the contained unsigned shorts and print them one per line:
convert image.tiff rgb: | perl -e 'my $str=do{local $/; <STDIN>}; print join("\n",unpack("v*",$str)),"\n";'
Sample Output
65535 # Full Red
0 # No Green
0 # No Blue
58253 # Lots of Red
0 # No Green
7282 # A little Blue
50972 # Moderate Red
0
14563
43690
0
21845
36408
0
29127
29127
0
36408
21845
0
43690
14563
0
50972
7282
0 # No Green
58253 # Lots of Blue
0 # No Red
0 # No Green
65535 # Full Blue
Another way of seeing the data may be using od and awk like this:
convert image.tiff rgb: | od -An -tuS | awk '{for(i=1;i<=NF;i++){print $i}}'
65535
0
0
58253
0
7282
50972
0
14563
43690
0
21845
36408
0
29127
29127
0
36408
21845
0
43690
14563
0
50972
7282
0
58253
0
0
65535
where the -An suppresses printing of the address, and the -tuS says the type of the data is unsigned short.
Perhaps a slightly simpler way in ImageMagick would be to use the txt: output format.
Using Mark Setchell's image:
convert -size 10x1 gradient:red-blue image.tiff
Using TXT: as
convert image.tiff txt: | sed -n 's/^.*[(]\(.*\)[)].*[#].*$/\1/p'
Produces:
65535,0,0
58253,0,7282
50972,0,14563
43690,0,21845
36408,0,29127
29127,0,36408
21845,0,43690
14563,0,50972
7282,0,58253
0,0,65535
or Using TXT: to include the pixel coordinates
convert image.tiff txt: | sed -n 's/^\(.*[)]\).*[#].*$/\1/p'
Produces:
0,0: (65535,0,0)
1,0: (58253,0,7282)
2,0: (50972,0,14563)
3,0: (43690,0,21845)
4,0: (36408,0,29127)
5,0: (29127,0,36408)
6,0: (21845,0,43690)
7,0: (14563,0,50972)
8,0: (7282,0,58253)
9,0: (0,0,65535)
Thank you all. The best answer I found was given by a student of mine using the package raster:
library(raster)
img <- stack(filename)
x <- as.matrix(raster(img, 1)) # here we specify the layer 1, 2 or 3
The only issue is that the function as.matrix of the package raster may be confused with the one from the base package, so it may be necessary to specify raster::as.matrix.
#Nate_A gave the answer, but three cents short of a dollar.
After
dim(tiger_array)
[1] 900 900 4
You get each channel color of first pixel by
tiger_array[1,1,1] # red
tiger_array[1,1,2] # green
tiger_array[1,1,3] # blue
Or if you prefer between 0 - 255
tiger_array[1,1,1]*255
tiger_array[1,1,2]*255
tiger_array[1,1,3]*255
I have a Qt/QML application with a C++ model and a QML visualisation.
At run-time (start-up), I get a warning
QML Item: Binding loop detected for property "xyz"
I see no obvious loop in my QML.
Can I enable more debugging to understand where this loop comes from? Other suggestions?
I usually do this by placing a breakpoint in the Qt code that prints the warning. For that, you need to have a Qt with debug symbols.
Searching for "Binding loop detected" in the Qt sources gives me QQmlAbstractBinding::printBindingLoopError(). Placing a breakpoint there will usually lead to a backtrace that gives a clear picture of the situation.
Update: David Edmundson has developed a little tool that displays a QML-only backtrace on binding loops, see his blog here. Under the hood is does exactly what is described here, only that it is nicely automated and wrapped in a Python script.
Example:
Rectangle {
id: parent
width: child.width + 1
height: child.height + 1
Rectangle {
id: child
anchors.fill: parent
}
}
Backtrace:
1 QQmlAbstractBinding::printBindingLoopError qqmlabstractbinding.cpp 178 0x7ffff6eb36da
2 QQmlBinding::update qqmlbinding.cpp 221 0x7ffff6eb9abe
3 QQmlBinding::update qqmlbinding_p.h 97 0x7ffff6eba354
4 QQmlBinding::expressionChanged qqmlbinding.cpp 260 0x7ffff6eb9e68
5 QQmlJavaScriptExpressionGuard_callback qqmljavascriptexpression.cpp 361 0x7ffff6eb223e
6 QQmlNotifier::emitNotify qqmlnotifier.cpp 94 0x7ffff6e9087a
7 QQmlData::signalEmitted qqmlengine.cpp 763 0x7ffff6e19a45
8 QMetaObject::activate qobject.cpp 3599 0x7ffff683655e
9 QMetaObject::activate qobject.cpp 3578 0x7ffff6836364
10 QQuickItem::widthChanged moc_qquickitem.cpp 1104 0x7ffff7a7ba49
11 QQuickItem::geometryChanged qquickitem.cpp 3533 0x7ffff7a6e9cd
12 QQuickItem::setSize qquickitem.cpp 6389 0x7ffff7a75f35
13 QQuickAnchorsPrivate::setItemSize qquickanchors.cpp 400 0x7ffff7a60d94
14 QQuickAnchorsPrivate::fillChanged qquickanchors.cpp 177 0x7ffff7a5fe0e
15 QQuickAnchorsPrivate::itemGeometryChanged qquickanchors.cpp 441 0x7ffff7a6106f
16 QQuickItem::geometryChanged qquickitem.cpp 3523 0x7ffff7a6e96c
17 QQuickItem::setWidth qquickitem.cpp 6091 0x7ffff7a74c1d
18 QQuickItem::qt_static_metacall moc_qquickitem.cpp 874 0x7ffff7a7b0dc
19 QQuickItem::qt_metacall moc_qquickitem.cpp 946 0x7ffff7a7b4d8
20 QQuickRectangle::qt_metacall moc_qquickrectangle_p.cpp 610 0x7ffff7c189c2
21 QMetaObject::metacall qmetaobject.cpp 296 0x7ffff680118b
22 QQmlPropertyPrivate::writeBinding qqmlproperty.cpp 1512 0x7ffff6e33ec3
23 QQmlBinding::update qqmlbinding.cpp 199 0x7ffff6eb992a
24 QQmlBinding::update qqmlbinding_p.h 97 0x7ffff6eba354
25 QQmlBinding::expressionChanged qqmlbinding.cpp 260 0x7ffff6eb9e68
26 QQmlJavaScriptExpressionGuard_callback qqmljavascriptexpression.cpp 361 0x7ffff6eb223e
27 QQmlNotifier::emitNotify qqmlnotifier.cpp 94 0x7ffff6e9087a
28 QQmlData::signalEmitted qqmlengine.cpp 763 0x7ffff6e19a45
29 QMetaObject::activate qobject.cpp 3599 0x7ffff683655e
30 QMetaObject::activate qobject.cpp 3578 0x7ffff6836364
31 QQuickItem::widthChanged moc_qquickitem.cpp 1104 0x7ffff7a7ba49
32 QQuickItem::geometryChanged qquickitem.cpp 3533 0x7ffff7a6e9cd
33 QQuickItem::setSize qquickitem.cpp 6389 0x7ffff7a75f35
34 QQuickAnchorsPrivate::setItemSize qquickanchors.cpp 400 0x7ffff7a60d94
35 QQuickAnchorsPrivate::fillChanged qquickanchors.cpp 177 0x7ffff7a5fe0e
36 QQuickAnchorsPrivate::update qquickanchors.cpp 431 0x7ffff7a60fc6
37 QQuickAnchorsPrivate::updateOnComplete qquickanchors.cpp 425 0x7ffff7a60f93
38 QQuickItem::componentComplete qquickitem.cpp 4593 0x7ffff7a70944
39 QQmlObjectCreator::finalize qqmlobjectcreator.cpp 1207 0x7ffff6ecab66
40 QQmlComponentPrivate::complete qqmlcomponent.cpp 928 0x7ffff6e38609
41 QQmlComponentPrivate::completeCreate qqmlcomponent.cpp 964 0x7ffff6e386ee
42 QQmlComponent::completeCreate qqmlcomponent.cpp 957 0x7ffff6e386a0
43 QQmlComponent::create qqmlcomponent.cpp 791 0x7ffff6e37edd
44 QQuickView::continueExecute qquickview.cpp 476 0x7ffff7b720d4
45 QQuickViewPrivate::execute qquickview.cpp 124 0x7ffff7b7101f
46 QQuickView::setSource qquickview.cpp 253 0x7ffff7b71426
47 main main.cpp 24 0x4033e4
In the backtrace, one can see that the anchors.fill anchor for the child item is calculated when loading the file (frame 35, 36). That causes the child item's width to change (frame 31), which causes a binding update (frame 25) for a binding on the "width" property (frame 17) on the parent item. That in turn forces a recalculation of the child anchors (frame 14), which changes the child's width (frame 10), which updates a binding (frame 4). That is the same binding that was already being updated in frame 25, hence a binding loop exists. One can see that the this pointer in frame 25 and frame 4 are the same, i.e. the same binding is updated recursively.
Thanks much for the receipt however it did not help me. In case somebody will need it, adding another possible solution. I was getting binding loops in ListView trying to set all items width and list width to item max value:
ListView {
implicitWidth: contentItem.childrenRect.width
delegate: listItem
}
Item {
id: listItem
width: Math.max(internalWidth, listView.implicitWidth)
}
Binding loop error appeared on items count update but not every time - only on some batch binding updates, while the is no actual binding loop. Was able to solve the issue by moving binding expression to Binding QML Type and adding delayed property to it:
Item { // Item causing binding loop
Binding on item_property_causing_loop {
value: <binding_expression>
when: <when_expression> // Optional however could also help
delayed: true // Prevent intermediary values from being assigned
}
}
So in my case it is:
Item { // Item causing binding loop
id: listItem
Binding on width {
value: Math.max(internalWidth, listView.implicitWidth)
when: index >= 0 // Optional however could also help
delayed: true // Prevent intermediary values from being assigned
}
}
I want to do a repeated measures ANOVA using ezANOVA() from the ez pacakge. My experimental designs is a two-way repeated measures design: soa (which has four levels) X congruency (which has three levels), and the data bellow is organized according to the above order (i.e., soa x congruency), with "subject" being the ID column.
My question is how should I use ezANOVA() if I already collapsed the data to cell means (the data bellow is after I collapsed the dependent variables for each subject according to soa x congruency)?
subject mrt1 mrt2 mrt3 mrt4 mrt5 mrt6 mrt7 mrt8 mrt9 mrt10 mrt11 mrt12
99 1039.3 1078.1176 997.5323 873.4615 1024 916.2 1061.0909 1008.7778 919.7879 1053 1052.9615 953.619
5203 1020.4545 1098.6667 911.2642 941.25 944.2857 976.1053 949 992.4167 870.4308 783.9091 852.1176 927.8852
5205 1373.7273 1074.2143 986.7397 1193.4615 1031.9545 1108.4789 1041.2727 1036.0625 989.0714 1180.5 908.9688 944.2024
5306 1012.375 1038.8421 949.4938 1320 1213.5714 1003.3133 1027.3333 970.2778 922.3939 1102 971.4286 943.5634
5307 1397.8333 1243.4 1114 1038 1187.6 1046.2588 1121.4 1376.0833 1080.6615 1075.2727 1159.2381 1060.6818
5308 809.9091 1193.75 1061.2895 923.7647 1128.4286 959.5783 771 1499.25 998.875 925.8462 1074.4 1022.3418
5309 1185 1257.2857 1230.1 1231.8333 1167.4545 1197.2051 1332.4444 1323.9 1334.359 1331.6 1418.5714 1198.8378
5410 1093.7 1154.2778 991.0147 1238.3846 1040.4783 1010.2 1009.3636 1161.6 1016.0946 1140.25 1020.1481 986.5312
5511 881.8182 1082.2 752.5455 1119.2222 969.25 848.8602 958.9444 850.3448 805.75 860.3 902.1579 758.1875
5512 879 1039 951.9138 866.4545 1146.7 898.2841 988 1078.3214 911.4203 1365.7273 1179 924.4928
5513 1239.3333 1063.9565 1013.1111 1018.25 1244.5625 1091.0847 1031.6667 1035.2381 1035.8727 1013.25 1016.5238 1045.9444
5614 1151.5385 1044.4545 1050.5303 1046.2727 1116.1154 1037.9577 1080.7647 1192.1053 1054.9688 1100 995.625 1065.1408
5715 1432 1307.1765 1152.6087 1124.7778 1305.9091 1125.7792 1210.5 1268.7647 1167.8 1199.5 1137.0476 1115.8462
5716 996.7143 1355 1108.4231 1026.8 1251.4286 1108.3333 1130.2857 1196.5333 1071 919.4545 1183.4286 1040.9796
5817 1046.9 1075.8276 865.7534 1058.1818 1101.9259 877.2143 923.5 1063.25 880.44 1050.2 984.24 919.8913
Any help will be greatly appreciated,
Ayala
I have created heatmap graphs using gnuplot.
I have data.dat:
avail reli perf
stop 181 20 121 10 34 20
jitter 18 20 17 20 13 20
limp 12 20 5 30 20 20
and gnuplot script:
set term pos eps font 20
unset key
set nocbtics
set cblabel "Score"
set cbtics scale 0
set cbrange [ 0.00000 : 110.00000 ] noreverse nowriteback
set palette defined ( 0.0 "#FFFFFF",\
1 "#FFCCCC",\
20.2 "#FF9999 ",\
30.3 "#FF6666",\
40.4 "#FF3333",\
50.5 "#FF0000",\
60.6 "#CC0000",\
70.7 "#C00000",\
80.8 "#B00000",\
90.9 "#990000",\
100.0 "#A00000")
set title "Faults"
set ylabel "Hardware Faults"
set xlabel "Aspects"
set size 1, 0.5
set output 'c11.eps'
YTICS="`awk 'BEGIN{getline}{printf "%s ",$1}' 'data2.dat'`"
XTICS="`head -1 'data2.dat'`"
set for [i=1:words(XTICS)] xtics ( word(XTICS,i) i-1 )
set for [i=1:words(YTICS)] ytics ( word(YTICS,i) i-1 )
plot "<awk '{$1=\"\"}1' 'data2.dat' | sed '1 d'" matrix w image, '' matrix using 1:2:($3==0 ? " " : sprintf("%.1d",$3)) with labels
#######^ replace the first field with nothing
################################## ^ delete first line
My output is:
Here I have range 1-20,30-39,...,100 or more)
Now I have to 2 values in every axis. e.g stop and avail have(181 and 20). the 181 is the count and 20 is percentages. I want to create graphs which have colors base on percentages and the labels on my graphs from the counts of data.
I have experienced create some graph using for and do some modulo to select the data. But here, I have not idea to create that graphs. Any suggestion for creating this? Thanks!
You can use every to skip columns.
plot ... every 2 only uses every second column, which is what you can use for the labels. For the colors, you must start with the second column (numbered with 1), and you need every 2::1.
Following are the relevant changes only to your script:
set for [i=1:words(XTICS)] xtics ( word(XTICS,i) 2*i-1 )
plot "<awk '{$1=\"\"}1' 'data2.dat' | sed '1 d'" matrix every 2::1 w image, \
'' matrix using ($1+1):2:(sprintf('%d', $3)) every 2 with labels
The result with 4.6.5 is:
I'm pretty new in statistics:
fisher = function(idxToTest, idxATI){
idxDependent=c()
dependent=c()
p = c()
for(i in c(1:length(idxToTest)))
{
tbl = table(data[[idxToTest[i]]], data[[idxATI]])
rez = fisher.test(tbl, workspace = 20000000000)
if(rez$p.value<0.1){
dependent=c(dependent, TRUE)
if(rez$p.value<0.1){
idxDependent = c(idxDependent, idxToTest[i])
}
}
else{
dependent = c(dependent, FALSE)
}
p = c(p, rez$p.value)
}
}
This is the function I use. It seems to work.
What I understood until now is that I have to pass as first parameter data like:
Men Women
Dieting 10 30
Non-dieting 5 60
My data comes from a CSV:
data = read.csv('***.csv', header = TRUE, sep=',');
My first problem is that I don't know how to converse from:
Loan.Purpose Home.Ownership
lp_value_1 ho_value_2
lp_value_1 ho_value_2
lp_value_2 ho_value_1
lp_value_3 ho_value_2
lp_value_2 ho_value_3
lp_value_4 ho_value_2
lp_value_3 ho_value_3
to:
ho_value_1 ho_value_2 ho_value_3
lp_value1 0 2 0
lp_value2 1 0 1
lp_value3 0 1 1
lp_value4 0 1 0
The second issue is that I don't know what the second parameter should be
POST UPDATE: This is what I get using fisher.test(myTable):
Error in fisher.test(test) : FEXACT error 501.
The hash table key cannot be computed because the largest key
is larger than the largest representable int.
The algorithm cannot proceed.
Reduce the workspace size or use another algorithm.
where myTable is:
MORTGAGE NONE OTHER OWN RENT
car 18 0 0 5 27
credit_card 190 0 2 38 214
debt_consolidation 620 0 2 87 598
educational 5 0 0 3 7
...
Basically, fisher tests only work on smallish data sets because they require alot of memory. But all is good because chi-square tests make minimal additional assumptions and are easier on the computer. Just do:
chisq.test(Loan.Purpose,Home.Ownership)
to get your p-values.
Make sure you read through and understand the help page for chisq.test, especially the examples at the bottom.
http://stat.ethz.ch/R-manual/R-patched/library/stats/html/chisq.test.html
Then look at a mosaicplot to see the quantities like:
mosaicplot(Loan.Purpose,Home.Ownership)
this reference explains how mosaicplots work.
http://alumni.media.mit.edu/~tpminka/courses/36-350.2001/lectures/day12/