Cluster position units in fastq file - bioinformatics

I have a fastq file with headers that include positions in the same format as the wikipedia example for fastq:
#HWUSI-EAS100R:6:73:941:1973#0/1
I know that x=941 and y=1973, but what are the units? Microns? Some illumina distance?
If there was another cluster at x=931 and y=1973, would they be 10 microns apart?
Thanks
EDIT:
Contacted illumina support and they said the information I needed, such as dimension of the tile viewing area or zoom of the microscope, was "considered proprietary," which is frustrating.

As a short term solution I've used the optimal cluster density provided here to estimate the size of 1 pixel in microns.
I have plotted the (x,y) positions of the clusters from a given tile to get a figure like this:
Steps:
Estimate the area of the viewing window from the number of clusters present
Convert from mm^2 to um^2
Convert to um^2 to radius of the window in um
Calculate the radius of the window in pixels along the x dimension
Divide the two to get the number of pixels per um
Code:
print 'For {} clusters, assuming optimal is {}'.format(num_clusters, num_optimal)
for perc in perc_optimal:
mm_sqr = num_clusters/(perc*num_optimal)
um_sqr = mm_sqr*1e6
um_radius = np.sqrt(um_sqr/np.pi)
px_radius = (pos['x'].max()-pos['x'].min())/2
px_to_um = px_radius/um_radius
out = ('At {}% optimal, tile r is {} um, pixel r is {} px,'
'so {} px is 1 um'.format(perc*100,um_radius,px_radius,px_to_um))
print out
Output:
For 531143 clusters, assuming optimal is 900000
At 10.0% optimal, tile r is 1370.59625256 um, pixel r is 13997 px,so 10.2123436963 px is 1 um
At 20.0% optimal, tile r is 969.157904453 um, pixel r is 13997 px,so 14.4424349589 px is 1 um
At 30.0% optimal, tile r is 791.314115365 um, pixel r is 13997 px,so 17.6882981464 px is 1 um
At 40.0% optimal, tile r is 685.298126279 um, pixel r is 13997 px,so 20.4246873926 px is 1 um
At 50.0% optimal, tile r is 612.949278085 um, pixel r is 13997 px,so 22.8354947145 px is 1 um
At 60.0% optimal, tile r is 559.543577023 um, pixel r is 13997 px,so 25.0150311339 px is 1 um
At 70.0% optimal, tile r is 518.036690306 um, pixel r is 13997 px,so 27.0193217236 px is 1 um
At 80.0% optimal, tile r is 484.578952226 um, pixel r is 13997 px,so 28.8848699179 px is 1 um
At 90.0% optimal, tile r is 456.865417519 um, pixel r is 13997 px,so 30.6370310889 px is 1 um
At 100.0% optimal, tile r is 433.420591057 um, pixel r is 13997 px,so 32.2942663288 px is 1 um
This is all very rough and I'd appreciate a different answer, but maybe this helps someone

Related

Best fitting rectangle with a variable number of small rectangles keeping aspect ratio

I need to find the optimal placement of a given N child rectangles keeping the aspect ratio of the father rectangle.
Use case is the following:
- the father rectangle is a big picture, let's say 4000x3000 pixels (this one can be rescaled).
- child rectangles are 296x128 pixels (e-ink displays of users)
The objective is to show the big picture across all the current number of displays (this number can change from 1 to 100)
This is an example:
Can happen that number of small rectangles will not fit the big rectangle aspect ratio, like if number of small rectangles is odd, in this case I can think to have like a small number (max 5) of spare rectangles to add in order to complete the big rectangle.
this seems to be a valid approach (python + opencv)
import cv2
import imutils
def split_image(image, boards_no=25, boards_shape=(128, 296), additional=5):
# find image aspect ratio
aspect_ratio = image.shape[1]/image.shape[0]
print("\nIMAGE INFO:", image.shape, aspect_ratio)
# find all valid combination of a,b that maximize your available badges
valid_props = [(a, b) for a in range(boards_no+additional+1) for b in range(boards_no+additional+1) if a*b in [q for q in range(boards_no, boards_no+additional)]]
print("\nVALID COMBINATIONS", valid_props)
# find all aspect ratio from previous combination
aspect_ratio_all = [
{
'board_x': a,
'board_y': b,
'aspect_ratio': (a*boards_shape[1])/(b*boards_shape[0]),
'shape': (b*boards_shape[0], a*boards_shape[1]),
'type': 'h'
} for (a, b) in valid_props]
aspect_ratio_all += [
{
'board_x': a,
'board_y': b,
'aspect_ratio': (a*boards_shape[0])/(b*boards_shape[1]),
'shape': (b*boards_shape[1], a*boards_shape[0]),
'type': 'v'
} for (a, b) in valid_props]
min_ratio_diff = min([abs(aspect_ratio-x['aspect_ratio']) for x in aspect_ratio_all])
best_ratio = [x for x in aspect_ratio_all if abs(aspect_ratio-x['aspect_ratio']) == min_ratio_diff][0]
print("\MOST SIMILAR ASPECT RATIO:", best_ratio)
# resize image maximining height or width
resized_img = imutils.resize(image, height=best_ratio['shape'][0])
border_width = int((best_ratio['shape'][1] - resized_img.shape[1]) / 2)
border_height = 0
if resized_img.shape[1] > best_ratio['shape'][1]:
resized_img = imutils.resize(image, width=best_ratio['shape'][1])
border_height = int((best_ratio['shape'][0] - resized_img.shape[0]) / 2)
border_width = 0
print("RESIZED SHAPE:", resized_img.shape, "BORDERS (H, W):", (border_height, border_width))
# fill the border with black
resized_img = cv2.copyMakeBorder(
resized_img,
top=border_height,
bottom=border_height,
left=border_width,
right=border_width,
borderType=cv2.BORDER_CONSTANT,
value=[0, 0, 0]
)
# split in tiles
M = resized_img.shape[0] // best_ratio['board_y']
N = resized_img.shape[1] // best_ratio['board_x']
return [resized_img[x:x+M,y:y+N] for x in range(0,resized_img.shape[0],M) for y in range(0,resized_img.shape[1],N)]
image = cv2.imread('image.jpeg')
tiles = split_image(image)
Our solutions will always be rectangles into which we have fit the biggest picture that we can keeping the aspect ratio correct. The question is how we grow them.
In your example a single display is 296 x 128. (Which I assume is length and height.) Our scaled image to 1 display is 170.6 x 128. (You can take out fractional pixels in your scaling.)
The rule is that at all points, whatever direction is filled gets filled in with more displays so we can expand the picture. In the single display solution we therefore go from a 1x1 rectangle to a 1x2 one and we now have 296 x 256. Our scaled image is now 296 x 222.
Our next solution will be a 2x2 display. This gives us 594 x 256 and our scaled image is 321.3 x 256.
Next we get a 2x3 display. This gives us 594 x 384 and our scaled display is now 512 x 384.
Since we are still maxing on the second dimension we next go to 2x4. This gives us 594 x 512 and our scaled display is 594 x 445.5. And so on.
For your problem it will not take long to run through all of the sizes up to however many displays you have, and you just take the biggest rectangle that you can make from the list.
Important special case. If the display rectangle and image have the same aspect ratio, you have to add to both dimensions. Which in the case that the image and the displays have the same aspect ratio gives you 1 x 1, 2 x 2, 3 x 3 and so on through the squares.

Distance to Object Webcam C920HD or use OpenCV calibrate.py

I am trying to determine the distance of an object and the height of an object towards my camera. Is it possible or do I need to use OpenCV calibrate.py to gather more information? I am confused because the Logitech C920HD has 3 MP and scales to 15 MP via software.
I have following info:
Resolution (pixel): 1920x1080
Focal Length (mm): 3.67mm
Pixel Size (µm): 3.98
Sensor Size (inches): 1/2.88
Object real height (mm): 180
Object image height (px): 370
I checked this formula:
distance (mm) = 3.67(mm) * 180(mm) * 1080(px) / 511 (px) * (1/2.88)(inches)*2.54 (mm/inches)
Which gives me 15.8 cm. Altough it should be about 60cm.
What am I doing wrong?
Thanks for help!
Your formula looks the correct one, however, for it to hold over the entire image plane, you should correct lens distortions first, e.g., following the answer
Camera calibration, reverse projection of pixel to direction
Along the way, OpenCV lens calibration module will estimate your true focal length.
Filling the formula gives
Distance = 3.67 mm * 180 mm * 1080/511 / sensor_height_mm = 1396 mm^2 / sensor_height_mm
Leaving sensor_height_mm unknown. Given your camera is 16:9 format
w^2 + h^2 = D^2
(16x)^2+(9x)^2 = D^2
<=>
x = sqrt( D^2/337 )
<=>
h = 9x = 9*sqrt( D^2/337 )
Remember the rule of 16:
https://photo.stackexchange.com/questions/24952/why-is-a-1-sensor-actually-13-2-%C3%97-8-8mm/24954
Most importantly, a 1/2.88" sensor has 16/2.88 mm image circle diameter instead of 25.4/2.88 mm. Funny enough, the true image circle diameter is metric. Thus the sensor diameter is
D = 16 mm/ 2.88 = 5.556 mm
and
sensor_height_mm = h = 2.72 mm
giving
Distance = 513 mm
Note, that this distance is measured with respect to the lenses first principal point and not the sensor position or the lens front element position.
As you correct the barrel distortion, the reading should get more accurate. It's quite a lot for this camera. I have similar.
Hope this helps.

How to compute horizontal gradient value?

So I want to measure the vertical edges of an image to use it later as depth cue for 2D to 3D conversion.
To do so I will have to compute the horizontal gradient value for each block to measure the vertical edges as follow:
̅ g(x,y) = 1/N ∑_((x',y')∈ Ω(x,y))〖g(x', y')〗
Where:
g(x',y') is a horizontal gradient at a pixel location (x',y'),
omega(x,y) is the nighborhood of the pixel location(x',y')
and N is the number of pixels in omega(x,y).
So Here is what I did on matlab:
I = im2double(imread('landscape.jpg'));
% convert RGB to gray
gI = rgb2gray(I);
[nrow, ncol] = size(gI);
% divide the image into 4-by-4 blocks
gI = mat2tiles((gI),[4,4]);
N = 4*4; % block size
% For each block, compute the horizontal gradient
gI = reshape([gI{:}],4*4, []);
mask = fspecial('sobel');
g = imfilter(gI, mask);
g_bar = g./N;
g_bar = reshape(g_bar,nrow, ncol);
I'm new to Matlab so I'm not sure if my code is expressing the equation in the right way.
Can you please let me know if you think it is correct? as I'm not sure how to test the output!
There's no need for you to decompose your image into 4 x 4 blocks. The horizontal gradient can be used with a Sobel filter or Prewitt filter, which is 3 x 3 and can directly be put into imfilter. imfilter performs 2D convolution / filtering with a specified mask / kernel for you, so tiling is not necessary. As such, you can just use imfilter with the mask defined through fspecial, and define N = 9. Therefore:
I = im2double(imread('landscape.jpg'));
% convert RGB to gray
gI = rgb2gray(I);
N = 9;
mask = fspecial('sobel');
g = imfilter(gI, mask);
g_bar = g./N;
From experience, increasing the size of your gradient mask won't give you much better results. You want to ensure that the mask is as small as possible to capture as many local changes as possible.

Matlab: Covariances for 1 pixel horizontal and vertical displacement of image

I have a gray scale image stored in a matrix Mat and am asked to extract the covariances for 1 pixel horizontal and vertical displacement.
I thought of using circshift and cov to extract the covariance.
Mat = magic(5); % this represents my gray scale image
MatHs = circshift(Mat,[0 1]); % horizontal displacement
MatVs = circshift(Mat,[1 0]); % vertical displacement
covMatH = cov(Mat,MatHs)
covMatV = cov(Mat,MatVs)
However, the result of covMatH and covMatV must be of size 1 by 1 where mine is 2 by 2.
Did I misapplied the cov functions or have I not understood the question correctly and this task must be solved completely different?
Since your image is 2-dimensional, you will be receiving a covariance matrix (link) of size 2*2. You have definitely solved the task of finding the covariance, but each element of the 2*2 matrix represents a different index. Lets say the matrix is [A B; C D]. A and D would represent variance of the inputs. B and C would represent cross-covariance between the inputs.

Traversing through pixels

Say that we have the following part of an algorithm, provided that I want to implement it in matlab:
k = 0;
while k<n
among all pixels that "belong to" a set, select that pixel
k = k+1;
How can I implement?
Determining if a pixel "belongs to" an image or a set of the image
Thanks.
Generally, in Matlab, image (gray/color) is represented as 2D/3D Matrix
For example,
I = imread("lena.jpg");
So, the size of the I is m x n for gray and m x n x 3 for color
Now if you want to select a pixel that "belongs to" a subset of image (rectangle) (xi yi) (w h).
The origin i.e., (1,1) pixel of the image is the top left corner.
Isub = I(xi:xi+w-1, yi:yf+h-1);
Also, you can just access pixels using pixel = I(i,j); ith pixel from top and jth pixel from left
If "belongs to" is something complex like all pixels which are red or something else, the approach/complexity would vary

Resources