Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can I show the max. and/or the min. value(s) of of a graph in a plot at their appropriate position automatically?
You can do this "semi-automatically" using the stats command. This command can extract some statistical values from a data set, but requires some reworking:
Extract the minimum and maximum y-values, assuming that your data file has two columns, the x-value in the first, the y-values in the second column
stats 'file.dat' using 2 nooutput name 'Y_'
This gives you the min/max y-values in the variables Y_min and Y_max, but not the corresponding x-value.
The previous step gives you only get the respective indices, which requires you to run stats again in order to get the x-values:
stats 'file.dat' using 1 every ::Y_index_min::Y_index_min nooutput
X_min = STATS_min
stats 'file.dat' using 1 every ::Y_index_max::Y_index_max nooutput
X_max = STATS_max
Set labels and/or points at the respective coordinates
set label 1 sprintf("%.2f", Y_min) center at first X_min,Y_min point pt 7 ps 1 offset 0,-1.5
set label 2 sprintf("%.2f", Y_max) center at first X_max,Y_max point pt 7 ps 1 offset 0,1.5
...
plot ...
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am making an OCR, I am using contours detection, I have extracted words and drawn bounding boxes but the problem is that when I crop the individual word, they are not in sorted order. I have tried sorting methods mentioned in this link to sort the contours but they work best on objects but in my case i want to make the order exact. sometimes the sorting is not the best solution it changes pattern of words as different words have different size of bounding boxes in same line and values of 'x' and 'y' varies with it. Now in same line, words with large bounding boxes are considered as one category and small ones are considered as other category and they get sorted in same fashion.This is the code to sort.
sorted_ctrs=sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0] + cv2.boundingRect(ctr)[1] *
im.shape[1] )
image of extracted bounded boxes
this is what I get after cropping from sorted contours
Is there any other method which can arrange my words so that it makes some sense?
You should start by separating out the different lines. When you have done that, you can simply process the contours left to right (sorted from x = 0 to x = width )
Start by drawing the found contours on a black background. Next, sum the rows. The sum of rows without words/contours will be 0. There is usually some space between lines of text, which will have sum = 0. You can use this to find the min and max height values for each line of text.
To find the order of the words, first look for the contours in the y range of the first line, then for the lowest x.
Input:
Code:
import cv2
import numpy as np
# load image and get dimensions
img = cv2.imread('xmple2.png',0)
h,w = img.shape[:2]
# sum all rows
sumOfRows = np.sum(img, axis=1)
# loop the summed values
startindex = 0
lines = []
compVal = True
for i, val in enumerate(sumOfRows):
# logical test to detect change between 0 and > 0
testVal = (val > 0)
if testVal == compVal:
# when the value changed to a 0, the previous rows
# contained contours, so add start/end index to list
if val == 0:
lines.append((startindex,i))
# update startindex, invert logical test
startindex = i+1
compVal = not compVal
You use the lineslist to further process the contours. The following code results in a list with the contours ordered based on position, which you can see by the list index written on the image:
# create empty list
lineContours = []
# find contours (you already have this)
x, contours, hier = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# loop contours, find the boundingrect,
# compare to line-values
# store line number, x value and contour index in list
for j,cnt in enumerate(contours):
(x,y,w,h) = cv2.boundingRect(cnt)
for i,line in enumerate(lines):
if y >= line[0] and y <= line[1]:
lineContours.append([line[0],x,j])
break
# sort list on line number, x value and contour index
contours_sorted = sorted(lineContours)
# write list index on image
for i, cnt in enumerate(contours_sorted):
line, xpos, cnt_index = cnt
cv2.putText(img,str(i),(xpos,line+50),cv2.FONT_HERSHEY_SIMPLEX,1,(127),2,cv2.LINE_AA)
# show image
cv2.imshow('Img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
You can instead print the contour index:
# write contour index on image
for line, xpos, cnt_index in (contours_sorted):
cv2.putText(img,str(cnt_index),(xpos,line+50),cv2.FONT_HERSHEY_SIMPLEX,1,(127),2,cv2.LINE_AA)
You could also create images for the separate lines:
# for each line found, create and display a subimage
for y1,y2 in lines:
line = img[y1:y2,0:w]
cv2.imshow('Img',line)
cv2.waitKey(0)
cv2.destroyAllWindows()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Position P is start with 7.56m and velocity V is 6(m/s). Acceleration A is -2 (m/s): How to get P when V becomes 0 and how to recalculate acceleration A for final P can be whole number by rounding off?
use equations of motion.
v2 = u2 + 2as
here,
v=0, u=6m/s and a=-2m/s2
So,
s = 9m
So, final position will be 7.56 + 9 = 16.56m
and since acceleration does not change, final acceleration = initial acceleration = -2m/s2
EDIT:
In that case, again use the same equation. Changed values will be:
v=0, u=6, s=9.44
So,
a = -1.907m/s2
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
the question is simple one object is moving from east-west with a velocity of v1 and another from south-north with velocity v2.
I just need the algorithm(formula) to calculate the shortest distance between them so I can write a program for it.
I do have distance between them and the meting point of their paths they are d1 and d2.
Assuming you are asking for 2-d space, at t=0, let the starting points be (d1,0) and (0,d2) on the coordinate axes. We can assume this because one object is always moving horizontally (E-W direction, along X-axis) and other vertically (S-N direction, along Y-axis). Now, after some time t, their positions will be,(d1-t*v1) and (0,d2-t*v2). (Speed-Distance-Time relation).
Now, distance between them at this time t will be,
D = d^2 = (d1-t*v1)^2 + (d2-t*v2)^2
So, differentiating both sides wrt t,
dD/dt = 2(-v1)(d1-t*v1) + 2(-v2)(d2-t*v2) ....(1)
For D to be minimum, dD/dt = 0 and second differential must be positive. Now, second differential :
d2D/dt2 = 2*v1^2 + 2*v2^2 which is positive for all real v1/v2. So, if `dD/dt = 0`, distance will be minimum.
So, equating (1) = 0, we get
t = (d1v1 + d2v2)/(v1^2 + v2^2)
So, get sqrt(D) at t = --above value-- and that shall be your answer.
PS: ask these type of questions on mathematics stackexchange.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In MATLAB, when I apply bwlabel on a binary image that has objects, I get a map that identifies which pixels belong to which object. However, there are certain objects that are close to each other that should really belong to the same object. (e.g. Three objects labelled 1,2,3 but they should be merged as one.) Can I merge these regions back somehow?
I have some code to get you started. This is taking a sample image from MATLAB which is full of text. After, what I did was I extracted out the bounding box of each of the letters in the text. Once I did that, I extracted their centroid location, as well as which pixels corresponding to each object within a bounding box. I used these two properties to help me get a rough clustering algorithm going.
Here are the steps to the algorithm I wrote:
Read in the image and extract each object's centroid and pixel locations for each object
Create an array that keeps track of which objects we need to visit
While there is at least one object that we need to visit:
Find any object that has this condition
Assign this object to a new cluster whose membership is just this object so far
Find the distance between this object's centroid to the other objects' centroids
If there are any centroids whose Euclidean distance is below some value, this belongs to the same cluster as the object we have declared in the beginning. Assign all of these objects to the same cluster.
Repeat Step #3 until there are no more clusters we need to visit.
This is the algorithm that I wrote:
clear all;
close all;
%//Read in text and extract properties
BW = imread('text.png');
s = regionprops(BW, 'Centroid', 'PixelList');
%//Create an array that tells us whether or not we have visited this
%//centroid
centroidVisited = false(length(s),1);
%//Create an array that tells us which object belongs to what cluster
membershipList = zeros(length(s),1);
%//List of all centroids for each object
centroidList = reshape([s.Centroid], 2, length(s)).';
%//Initialize cluster count
clusterNumber = 1;
%//Threshold to figure out what is near
distThreshold = 30;
%//Map that gives us which pixel belongs to which cluster
map = zeros(size(BW));
%//If there are any objects we haven't visited...
while (any(centroidVisited == false))
%//Find one object
ind = find(centroidVisited == false, 1);
%//Extract its centroid
cent = s(ind).Centroid;
%//Grab pixels where this object is valid
pixelLocs = s(ind).PixelList;
%//Find Euclidean distance squared between this centroid to all the
%//other centroids
distCentroids = sum(bsxfun(#minus, cent, centroidList).^2, 2);
%//Find those locations that are lower than the centroid
%//Also ensure that we filter out those locations that we have already visited
belowThresh = find(distCentroids < distThreshold*distThreshold & ...
centroidVisited == false);
%//Mark them as visited
centroidVisited(belowThresh) = true;
%//Assign their membership number
membershipList(belowThresh) = clusterNumber;
%//For each object that belongs to this cluster, mark them with this
%//membership number
for k = 1 : length(belowThresh)
placesToMark = s(belowThresh(k)).PixelList;
map(sub2ind(size(BW), placesToMark(:,2), placesToMark(:,1))) = ...
clusterNumber;
end
%//For the next cluster
clusterNumber = clusterNumber + 1;
end
%//Create a colour map that is the same size as the number of clusters
colourMap = jet(clusterNumber);
%//This colour map will contain what letters belong to what cluster (colour
%//coded)
colourMapRed = colourMap(:,1);
colourMapGreen = colourMap(:,2);
colourMapBlue = colourMap(:,3);
mapColumn = map(:) + 1;
redPlane = colourMapRed(mapColumn);
greenPlane = colourMapGreen(mapColumn);
bluePlane = colourMapBlue(mapColumn);
redPlane = reshape(redPlane, size(BW,1), size(BW,2));
greenPlane = reshape(greenPlane, size(BW,1), size(BW,2));
bluePlane = reshape(bluePlane, size(BW,1), size(BW,2));
clusterMapColour = cat(3,redPlane, greenPlane, bluePlane);
figure;
subplot(1,2,1);
imshow(BW);
title('Original Image');
subplot(1,2,2);
imshow(clusterMapColour);
title('Clustered Image');
This is the image that I get:
The variable clusterMapColour will illustrate which objects belong to which cluster in a colour coded map. What you are really after is the map variable. This is pretty much like the output of bwlabel, except that objects with the same label k belong to the cluster k. You will have to play around with the distThreshold variable to get the results that you want. I chose 30 as something arbitrary and something to start with. This means that anything with a 30 pixel radius of a centroid gets classified as the same membership number as that centroid. Also, we keep track of what objects have already been visited so that they don't get reclassified again as we move along in the image.
Good luck!
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
(This question could be better off on math, but im not sure)
http://i.imgur.com/TVINP.png
This is probably very simple but the way I'm thinking of doing it doesn't seem very easy and there must be a simpler method.I've got an image and I want to find some points that fall on a line so in this example image below the starting point of my line is (39,75) and the ending point is (75,142) from there I want to find 5 points (or any number really 5 is just an example) that are all on that line.
Is there some equation I can use that will get me a certain amount of points given any start and end coordinates?
yes.
suppose (x0,y0) and (x1,y1) are the starting and ending points on the line.
t*(x0,y0) + (t-1)*(x1,y1) are also going to be points on that line where t ranges from 0 to 1.
note:
if t = 0, you get (x0,y0)
if t = 1, you get (x1,y1)
if t is any value inside (0,1) you get that "percentage" of the way from (x0,y0) to (x1,y1)
(if t = 0.5, you are halfway between the points)
this is what is often called "tweening" in computer graphics
Yes. Your line segment can be described by this equation:
x = 39 + t * (75 - 39)
y = 75 + t * (142 - 75)
where, t can take on any value between 0 and 1.
So, to get random points on the line, just choose a random value for t (between 0 and 1), and calculate what x, y are.
The idea is, x is traveling from 39 to 75, while y is traveling from 75 to 142, and t represents the fraction of travel that has been completed.
A line can be defined by the function y = mx + b where x and y are coordinates on the cartesian plane, m is the slope of the line defined by (y2 - y1)/(x2 - x1), and b is the point where the line intersects the y-axis.
Given that information and two points on the line, you can fill in the blanks with some basic algebra to determine a function that defines the line. Note that if your coordinate plane for the image places (0, 0) in the top left corner, you may have to flip the sign of the y-coordinate.