Using Edge Detection Filters VB Help

You can use another type of custom filter to extract the edges of an image. Because an image contains both horizontal and vertical edges, the two types of edge detection filters d,etect the corresponding types of edges. The horizontal edge detection filter detects and extracts horizontal edges by subtracting a row of pixels above tne center pixel from the corresponding pixels below the center pixel. The vertical edge detection filter subtracts columns of pixels instead of rows, andits definition is shown in Figure 7.15. Figure·7.16 show the result.

In the Cfilters folder in this chapter’s folder on the CD, you’ll find a few additional figures that show pairs of filters and processed images. These figures show some special effects implemented with custom filters: two “neon” effects, an “outline” effect, and a “shake” effect. Open the images in the Cfilters folder to see some interesting
effects you.can create with the custom filter option of the Image application.

Using Palettes

In concluding this chapter,let’s briefly review a technique for accessing palette colors. Although specifying colors with. the RGBOfunction is straightforward, you can’t be sure that the color displayed is the same as the one you specified with the RGBO function’s arguments. For example, you may draw a yellow circle .on top of an image and see a “greenish” circle. This is because computers that can’t display more than 256 colors must approximate Color values with existing colors. Therefore, if the bitmap displayed doesn’t already contain a yellow tone, . then the computer may try to approximate the yellow color with a tone that’s not even close.
Most of the time, your computer will approximate colors quite nicely. Sometimes, however, you must be able to access the colors in the current-palette so that the color you specify in your code is the color being displayed. Visual Basic provides  mechanism for accessing the entries of the current palette by index. (There are many Windows API functions for manipulating palettes, but this is a complicated topic that won’t be covered in the book.) To retrieve the palette entry Plndex, use the expression:

clr is a Long Color value, which can be used in your code in the place of theRGBOfunction. For example, to paint a point with the 120th entry I D. the current palette, call the PSet method with the following arguments (the first entry in the palette has.zerp index):

Form1,.Pset (X, Y), &H1000000 + 119

Forms and Picture Boxes don’t have a default palette. You must first load a palette by assigning it to the Palette property of the control. To load a palette to a Form, assign the name of the file with the palette to the Form1.Palette property  and set the Palett~Mode property to 2 (Custom Palette). For more information on these two properties, see the section “True Color and Palettes” of Appendix C:
A palette file is a BMP or DID file with a single pixel and a 256-color palette. Most image processing applications let you edit an image’s palette. To create a palette file, open an image that contains many of the colors you like with an application like Paint Shop Pro and look at its palette. Edit it if you like, or create an entirely new palette. Then resize the image to a single pixel (1 x 1) and save it as BMP or DID file. This single pixel image contains its own palette and you can use it to load the palette onto a Form. Every PictureBox and ImageBox control on the form will use this palette.

In the PalColor folder in this chapter’s folder on the CD, you’ll find the sampleprojects of this section and a number of palette files (stored in th e PIB folder). They are the Bluewhit.dib, Earth.dib, ManyClrs.dib, Mono.dib, MoonLite.dib, Neon.dib, SeaSky.dib, and Smooth.dib files, and they are all single pixel tmages with the desired palette. You can open these files with an image processing application and see their palettes.

VB6 at Work: The PaletteColor Project

TIle PaletteColor project, shown in Figure 7.17, demonstrates how to load a palette on a Form and access its colors (you’ll find the project in the PalColor folder under this chapter’s folder on the CD). The PaletteColor application loads a user-specified palette on a Picture Box control and then creates a grid of 256 small boxes, arranged in 16 rows of 16 columns. Each box on the grid is colored with a different palette color. It paints the upper left box with the first entry in the palette, the one below with the next entry in the palette, and so on. When the first column is exhausted, the program continues with the second column, and so on.

Every time the user selects a new palette with the Load Palette button, the following lines are executed, which create the grid and paint every box with a different color.

Creating the Color Grid

When the user selects a color by clicking in a box, the following lines read the color’s value and display its RGB components in the Label controls at the bottom of,theForm.

Displaying a Color’s Components

VB6 at Work: The PaletteGradient Project

If you attempt to display a gradient on a palette system you may see three or four different colors only. Obviously, this isn’t much of a palette. To see how the gradient will be drawn, set your monitor’s color depth to 256 colors (right-click on the Desktop, and from the shortcut menu select Properties :>Settings) and run the ChGrads application. To create smooth gradients on palette systems, you must create a custom palette, load it on the PictureBox control or Form, and then access’ the palette’s colors. The PaletteGradient project, shown in Figure 7.18, does exactly. that. (The PaletteGradient project is called PalGrad and can be found in the PalColor folder on the CD. The project uses the palette files in the DIB folder.)

The PictureBox control on which the gradients are drawn has a width of 472 pixels. The gradient is drawn with 236 vertical lines, each one having a width oj two pixels. The program makes use of 236 of the palette’s 256 colors, The’first 10 and last 10 colors in the palette are reserved, by Windows for displaying the standard colors (the colors used for the title bars.scrollbars, and other elements-of the user interface are always the same and they aren’t affected by the palette). If you open the palettes in the OIB folder, you’ll also see the standard Windows colors at the beginning and end of the palette. Since you don’t have control over the basic of colors in the palette, you shouldn’t use them. The gradient is drawn by the DrawGradientO subroutine each time you select a new palette by clicking on another-Option button at the bottom of the Form. The DrawGradientO subroutine is shown in Code 7.15

The DrawGradientO Subroutine

If you want to add a gradient (or any other pattern with. many custom colors), create the appropriate palette and load it on the Form. Then, you can access the palette’sentries and make sure that the gradient will appear with its proper colors on the palette system. You’ll find more examples, as well as a discussion of the Form’s palette-related properties-in Appendix C on the CD, Using Multimedia Elements to Enhance Applications.

Posted on November 5, 2015 in Manipulating Color and Pixels with Visual Basic

Share the Story

Back to Top
Share This