Brackets Tutorial 5: Advanced Cardboard Aided Design: Difference between revisions
Line 136: | Line 136: | ||
In Inkscape the process is also pretty straightforward. Import the image and select the "Trace Bitmap" tab. This will give you a slider with which to adjust the threshold level up and down. In the image below, the threshold has been placed just at the point where some of the black areas start to appear white. The level is given as 0.33, which corresponds to a digital value of 84. Click the "Apply" button. Then you will need to select the background, which is the original image, and delete it. Then export the image either as .JPG or as .PNG. | In Inkscape the process is also pretty straightforward. Import the image and select the "Trace Bitmap" tab. This will give you a slider with which to adjust the threshold level up and down. In the image below, the threshold has been placed just at the point where some of the black areas start to appear white. The level is given as 0.33, which corresponds to a digital value of 84. Click the "Apply" button. Then you will need to select the background, which is the original image, and delete it. Then export the image either as .JPG or as .PNG. | ||
[[File:Tut5_inkscape_threshold_capture_labeled.png|center| | [[File:Tut5_inkscape_threshold_capture_labeled.png|center|750px|inkscape screencap]] | ||
It is important here to point out subtle differencse between the images shot with a white background and a gray background. In the figure below, the upper image had the white background and the lower image had the gray background. Each has been thresholded to give optimal results: 127 for the upper image and 85 for the lower image. | It is important here to point out subtle differencse between the images shot with a white background and a gray background. In the figure below, the upper image had the white background and the lower image had the gray background. Each has been thresholded to give optimal results: 127 for the upper image and 85 for the lower image. |
Revision as of 15:09, 9 December 2024
Link to: Bracketage Main Page
Introduction
This tutorial covers a photographic technique for gathering data from an existing part. As an example, we use one of the sheet-metal blanks from Tutorial 2b. Digital photography straddles the digital and analog domains. We will show how to gain the best precision from using the software tools to convert a photo into a CNC program.
To track the precision of the process, we will use the length of the bracket as an indicator. The photograph below shows that the bracket is 12.66 inches long.
Photography
A good quality camera stand (Kaiser RS-1) can be found in the Photo/Design shop at Artisans Asylum. With the camera mount moved all of the way to the top, the field of view with an ordinary camera is about 40 inches (1 meter) wide and about 1/2 that in height.
A dark, unfinished steel bracket blank from Tutorial 2b has been placed on a white paper background and photographed with an undistinguished digital camera. Some care has been taken to avoid the presence of shadows inside the slot in the lower right of the blank, which is about 1/8 inch thick.
The resolution of the image is 35.44 dpi, or 0.717 mm per pixel. This calculation can be done in a variety of ways. The most immediately available method is to photograph a ruler along with the pattern, as shown. By counting the pixels between the ends of the ruler, the resolution can be found. A pixel count can be obtained using the "Paint" application, which displays the numerical cursor location in the lower-left corner of the window. The 18-inch ruler in the image is 638 pixels long: 638/18 = 35.44 dpi. This resolution calculation is for illustration only. It's not needed for the process described here.
Image Analysis
As in the previous tutorial, we need to convert the image into a high-contrast black-and-white bitmap. Tutorial 4 showed how to use "Paint" to do this be a "sledgehammer" approach that had few options. The application sets up a threshold at the 50% gray level and sets all pixels below that value to black, and the rest to white.
Let us examine in detail what happens to the image when we do this.
Below are two images taken with the same camera at different times. Notice that the demarcation between the light areas and the dark area isn't sharp. This isn't an error in focusing the camera. It is a consequence of modern digital photography, which purposely blurs out a boundary like this using a scheme known as "anti-aliasing." This creates a boundary that appeals to the viewer's eye without the jagged steps that the individual pixels make with one another. Unfortunately this scheme imposes a loss of accuracy that we must seek to regain when we use a consumer camera as a measuring instrument instead of an artistic tool.
More sophisticated image processing applications such as Adobe Photoshop or Inkscape allow you to set a threshold at some other value besides the exact middle, and the following discussion will show why this is desirable.
A detail of the original images are displayed below, left. Next to each there is a histogram of the pixel values in the image, captured using Adobe Photoshop.
On another occasion the same piece was photographed with the same camera in lower light, and using a grayish paper towel as a background instead of a piece of white paper.
A comparison of the two images shows that the dark portion is about the same value while the backgrounds have different values. The histograms for both images show two peaks (a "bimodal" distribution) with little or no overlap, where the curve to the left indicates the values in the dark part of the image and the curve to the right indicates the values in the light part of the image.
Examine the histograms more closely. Notice that the curves in the graphs aren't sharply defined, but rather they have relatively long "tails" that extend in both directions away from the peaks. Notice also that in the first image, the threshold level (50% gray) falls in a relatively unpopulated region between the tails of the two peaks in the histogram. In the second image the threshold value falls upon part of the tail of the curve on the right, corresponding to the background.
For the first image, the division between "black" and "white" areas is accurate. In the second image we find points that should be in the "white" population will erroneously be assigned to color "black." The pixels in this population are all located within the boundary between the light and dark regions. The error causes the dark region of the image to bleed outward beyond the boundary where it ought to be confined, making the piece longer and the holes smaller.
It should be obvious from this discussion that the best results are obtained by making the object as dark as possible and using a background that is as light as possible.
Monochrome Bitmap
The image below is a rendering of the first ("good") image that has been saved as a monochrome bitmap as in Tutorial 4. The corresponding rendering from the second ("bad") image isn't shown because the two are almost indistinguishable.
Please note that the images we are considering were photographed at relatively low resolution, between 35 and 36 dots per inch. The resolution could be improved by moving the camera closer to the object, reducing the width of the field of view. A camera with a higher pixel density would accomplish the same goal.
Notice the jagged outline of potions of the image. This effect is known as "aliasing." Consumer-quality digital cameras apply an algorithm ("anti-aliasing") to the images to smooth out the effect and make the image more pleasing to the eye. The operation of converting to a monochrome bitmap has re-aliased the image, reversing the effect of the algorithm.
Crop the Image
To preserve the scale of the data in the image, we will crop it in two steps. Ultimately we will eliminate all of the features in the image that are neither background nor the pattern of interest. To preserve the scale, we first crop the image as shown in the picture below left, keeping the ruler in the image temporarily.
We can inspect the ruler in the cropped image and find that the field is 14-1/2 inches wide. As a check we can look in the "Properties" window for the file and find the cropped image is 514 pixels wide. The quantity 514/14.5 = 35.45 dpi. This coincides with the earlier calculation of the resolution from the whole image, except for a small round-off error.
Having found the width of the field we crop the image a second time, keeping the width constant but eliminating the ruler from the bottom margin of the image. This is the file that we will import into Torchmate CAD that, like "Paint," sets a threshold to 50% gray when vectorizing.
Vectorization of the Bitmap
To transform the bitmap data into a tool path for the CNC plasma cutter, we import each cropped image into Torchmate CAD in the same fashion as in Tutorial 4. Click on the icon to open a blank page. Pull down the "Import" item from the "File" menu.
After picking the desired image file, a box will appear that seems to allow the user to select a threshold value for the B/W conversion. Don't get your hopes up. Changing the value here has no effect.
A rendering of the selected image should appear in the window. In the previous section when we cropped the image we measured the width of the field. For the "good" image this value was 14.5 inches. This value is used to scale the image in the toolbar across the top of the window.
To create an outline of the dark area, select the Accuscan tool along the left margin of the window.
Check the settings with the small button indicated on the tool bar. The "Tolerance" setting must be relatively high for an accurate conversion, however the highest setting incorporates the re-aliased jaggies into the tool path.
To complete the process of converting the image to an outline for the tool path, click on the indicated button on the tool bar. Other tools on this bar aren't relevant here.
This exits the Accuscan mode in the program and shows the calculated outline. When selected, the outline becomes red. For this image, please note that the overall length displayed in the toolbar has changed. Now it displays the overall length of the outline, which is 12.609 inches, or about 0.051 inches smaller than the original.
If we perform the same operation on the "bad" image with the darker background, the resulting length of the part becomes 12.785 inches, or 0.125 inches larger than the original. In the former case, the error amounted to a little less than two pixels; in the latter case it was about 4-1/2 pixels.
Students of information theory may recall the "Nyquist" limit for sampling a signal. This limit expresses the resolution of a sampled function based on the sampling rate, and it corresponds to about two times the period of sampling. By careful staging of the digital photography we have achieved this limit with a minimum of software manipulation. The second image can be made more accurate, but only through additional processing e.g. with Photoshop or Inkscape.
Alternate paths to Accuracy
We're not done yet!! There are different ways to act on the information we just gathered. That is: We know how big the part is supposed to be and how big the scanned bitmap resolved in the software.
Scale the Part
By far the easiest way to correct the length of the part is to type in the correct dimension into the box circled in the previous illustration. This will change the scale of the part and make its length what you specify.
If the change is small, such as what we found in the first image example, then this should produce acceptable results. For the best accuracy, this method causes a problem.
When you scale a part, all of the features change in size by the amount you specify. If you make the part larger, the holes in the part also get larger and they get further apart.
This is different from the bleed effect produced by anti-aliasing the image. With this algorithm, the part gets larger but the holes get smaller and they stay the same distance apart (center-to-center.)
Re-Threshold the Image
For the image shot with a gray background, the dimensional error is too large to fix by scaling. If you have access to a relatively sophisticated image-processing tool (not Paint) then you can adjust the threshold level to the B-W conversion of the image to some value besides 50% gray (this is equal to 127 in the digital value.)
For the image with the gray background we find that setting the threshold to 85 instead of 127 gives better results. In this case the part is rendered at 12.688 inches in length. We can be confident that the holes are also the right size and in the right locations with this method.
In Photoshop this process is quite straightforward: Open the image for editing, select the whole area (CTRL-A) and select the "Levels" tool (CTRL-L). The display will show the histogram as illustrated at the top of this document, and you can drag the white and black arrows to an area between the two peaks that is relatively unpopulated, and place them close together. Save the file as .JPG or .PNG.
In Inkscape the process is also pretty straightforward. Import the image and select the "Trace Bitmap" tab. This will give you a slider with which to adjust the threshold level up and down. In the image below, the threshold has been placed just at the point where some of the black areas start to appear white. The level is given as 0.33, which corresponds to a digital value of 84. Click the "Apply" button. Then you will need to select the background, which is the original image, and delete it. Then export the image either as .JPG or as .PNG.
It is important here to point out subtle differencse between the images shot with a white background and a gray background. In the figure below, the upper image had the white background and the lower image had the gray background. Each has been thresholded to give optimal results: 127 for the upper image and 85 for the lower image.
Notice in the lower image the slot in the lower right has been closed off. This will need to be corrected by drawing a white stripe (e.g. in "Paint") to reopen the slot before it is converted into a tool path. Also notice a tiny white dot in the lower center of the lower image. This will also have to be colored in.
Notice also that none of the holes are perfectly round. Some of this may be due to the thickness of the original pattern or asymmetry in the lighting. If exact circularity is required this will have to be corrected manually with "Paint" or some other program.
Apply Tool Compensation
There are two stages where the user can offset the tool path by a fixed distance. This has nearly the same effect as re-thresholding the image. If the tool path is displaced inwards, the part becomes smaller, holes become larger, and they stay the same distance apart.
Using Torchmate CAD, we will create a tool path in much the same fashion as shown in Tutorial 4
In the normal course of processing Torchmate CAD applies an offset to the tool path to compensate for the width of the kerf cut by the torch. The setting in the program is 0.059 inches. If you select a "Male" tool path, the cut is displaced outwards by the set thickness. If you choose "female" the cut is displaced inwards. If you select "Online" the tool path has no displacement applied to it.