LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

i want to crop the rectangle shape from an image?what is the method for it?

i have two images.one is my original image and second one is my rectangle template image.now using the coordinates of template image i want to extract or crop that region from original image.how it is possible?please guide.

0 Kudos
Message 1 of 36
(8,202 Views)

my template rectangle image is already extaracted from orginal image by appling many functions and filling holes.now i want to crop it on original image

0 Kudos
Message 2 of 36
(8,154 Views)
do you have vision toolkit ?
0 Kudos
Message 3 of 36
(8,118 Views)

yes sir

0 Kudos
Message 4 of 36
(8,111 Views)

extract.jpgdo not forget to use Imaq before this vi otherwise your original image will be lost 

0 Kudos
Message 5 of 36
(8,090 Views)

One way to "extract" rectangular regions from an Image is to use the concept of a "Region of Interest", or ROI.  In the LabVIEW Vision toolkit, Image displays have a set of "tools" associated with them.  Two of these (symbolized by a Rectangle and by a Rotated Rectangle) are the "Rectangle Tool" and the "Rotated Rectangle Tool" (catchy names, right?).  What these tools let you do is to click-and-drag to drop a (default Green) rectangle on your Image that you can size, move, Rotate (if a "Rotated Rectangle"), and place over an area on your Image that you want to extract.

 

This is where the IMAQ Extract Tetragon function comes in.  This takes three inputs -- the Original Image from which you want to extract a four-sided (Tetragon) sub-Image, a newly-created Image (IMAQ Create) where the extracted Image will be placed (unless you want to discard the original Image and replace it with the cropped sub-Image), and the "Tetragon".  If you created an ROI with the ROI tool, you can simply wire it to this Input.

 

But where do you get the ROI?  What do you wire to the ROI Descriptor input of Extract Tetragon?  Remember that you created the ROI by "drawing" it on an Image Control (for want of a better name, I'm going to call this Image Control "Image Control").  If you create a Property Node for Image Control, you will see that one of the Properties is called ROI.  Aha!  This is the origin of the ROI wire, which can be saved in a Shift Register until needed, or wired directly to the Extract Tetragon Input terminal.

 

OK, but how do you know that there is anything "present" in the ROI Property?  What if you haven't yet drawn an ROI?

 

To answer this, we need to ask "What is an ROI?"  The answer is that it is a LabVIEW IMAQ data structure that describes several things.  Actually, it will help to explain why you want a Region of Interest.  I'm sure you realize that when you do Image Analysis, what you are really doing is computation on an array of Pixels, whose (integer) coordinates correspond to (integer) locations within the Image, and whose values are 8, 16, or 32 (usually) bit integers corresponding to shades of grey or color intensities.  That can be a lot of data to process, particularly if you know that you only want to do computations for a small "Region of Interest" within the larger Image.  Wouldn't it be nice, if your ROI were a 32-pixel square region from a 640-by-480 (fairly small) Image to only work with the 1024 pixels in the ROI instead of the 307,200 pixels in the entire Image?  The code should run (at least) 300 times faster.  That's why one "Extracts" image data using ROIs.

 

Ah, but there's a complication.  What if you are using a tilted rectangle?  If we take the same 32-pixel square region and rotate it by 45°, several things happen.  First, most (all?) of the Pixels are likely to no longer have integer coordinates, so how will the intensity of the Extracted Image be obtained?  Second, the region of the original Image that must be examined will be larger (to take into account the now-rotated coordinates of the corners (a square of about 45.3 pixels -- oops, there's another "fractional pixel measurement).

 

If you examine the ROI cluster, it has two main sections.  One, called the "Global Rectangle", is precisely the Pixel coordinates of the Rectangle in the original image that contains the ROI.  The other, "Contours", describe the properties of the ROI itself.  If no ROI has ever been defined on the Image, the Global Rectangle (which contains the Pixel coordinates of two opposite corners of the Rectangle containing the ROI) will be either an empty array or an array of all Zero values.  Testing this component can tell you if you have a valid ROI.  Note, however, that the ROI remains a property of the Display -- it retains its value until a Clear ROI Method is invoked on the Image Control.

 

Bob Schor

0 Kudos
Message 6 of 36
(8,059 Views)

I hate to write thousand of the words for some simple thing just i want to mention two more thing and try to be forthcoming as I can be do
1 if your means from template image is size of image you have to read your template size and try to make appropriate rectangle points to extract part of original image in same size
2 but if your template image is mask ! then you have to use IMAQ MaskToROI VI
to create related ROI and then use it to extract with that vi I mentioned (outside of rectangle should be black and inside should be white )
3but about Global Rectangle try to do not use it to extract because this Rectangle is not ROI indeed it is just smallest Surrounding Rectangle aground your ROI
it will not make correct rectangle for other type of ROI like Rotated Rectangle
when you find out that your ROI have fractional value you have to increase size of original image and you have to calculate minimum Increasing in size to get ROI without error and exactly in ROI size that you select

0 Kudos
Message 7 of 36
(8,042 Views)

sir i cant understand that how property node gives roi.where i have to connect input of property node?please explain in detail.and also what is roi descriptor.it has four parameter iglobal rectangle ii Id iii type iv coordinates .please explain the meaning of each one.

0 Kudos
Message 8 of 36
(8,019 Views)

OK, I thought I was being pretty clear, but maybe not.  Here is an Image Control, called Images.  It's a 16-bit Grayscale Image, and I've used the Tilted Rectangle ROI Tool (shown highlighted in the Toolbar on the left of the Image) to create a Tilted Rectangle (shown in Green) around a fuzzy "Region of Interest".

 

When I draw an ROI on an Image Control, it becomes a property of the Control.  I can access it (once it's been drawn) by using a Property Node, as shown below.  Any place I need to use an ROI (as in Extract Tetragon), I can simply wire it to the ROI output of this Property Node.

 

ROI on Image.pngROI Property Node.png

You don't really need to worry, or even think about, the components of the ROI.  I apologize for giving you "too much to think about" by mentioning the Global Rectangle -- this turns out to be "interesting" or "useful" if you want to know if an ROI even exists.  For example, if I have a sub-VI (which I'm about to show you) with an ROI Input, how do I know that I even wired anything to it?  Well, if the ROI doesn't have an array with non-zero components in its Global Rectangle, it doesn't exist!

 

So here's a little sub-VI that assumes you pass in an Image (such as the one shown above in the Images control, an ROI (such as the one outlined above), and you want to calculate the mean intensity of this "extracted rectangle from your Image".  This little sub-VI does that.  The two U16 Image Pool functions get and release a temporary U16 Image Buffer to hold the Pixels designated by the ROI.  The IMAQ Extract Tetragon takes the Input Image and the Input ROI and extracts the Pixels that lie within the ROI into a rectangular array.  The next IMAQ function takes that "image" and returns a 2D array of U16, i.e. all of the Intensity data.  The next few functions compute and return the Mean Intensity (I could have used the Mean function, but it doesn't like 2D arrays, so I just "did it directly").

 

Compute Mean Intensity.png

Hope this is helpful.

 

Bob Schor

0 Kudos
Message 9 of 36
(7,991 Views)
could you attach you both image (origin and template ?
then I could give you appropriate code to use
0 Kudos
Message 10 of 36
(7,980 Views)