Found 26504 Articles for Server Side Programming

Calculating the median of all pixels for each band in an image using the Pillow library

Prasad Naik
Updated on 17-Mar-2021 08:16:31

886 Views

In this program, we will calculate the MEDIAN of all the pixels in each channel using the Pillow library. There are a total 3 channels in an image and therefore we will get a list of three values.Original ImageAlgorithmStep 1: Import the Image and ImageStat libraries. Step 2: Open the image. Step 3: Pass the image to the stat function of the imagestat class. Step 4: Print the median of the pixels.Example Codefrom PIL import Image, ImageStat im = Image.open('image_test.jpg') stat = ImageStat.Stat(im) print(stat.median)Output[41, 43, 40]

Drawing average line in histogram in Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 08:18:07

2K+ Views

We can plot some expressions using the hist method. After that, we will plot the average graph for the expression using the plot method and bins that are returned while creating the hist.StepsGet the data for x using some equations, set num_bins = 50.Create fig and ax variables using subplots method, where default nrows and ncols are 1.Get n, bins, patches value using ax.hist() method.Plot average lines using bins and y data that is obtained from some equations.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Set a title for the axes.Using tight_layout(), we can adjust the ... Read More

Performing a closing operation on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:17:42

2K+ Views

In this program, we will perform the closing operation using the cv2.morphologyEx() function. Closing removes small holes in the foreground, changing small holes of background into foreground. This technique can also be used to find specific shapes in an image. The function we will use for this task is cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel).Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image. Step 3: Define the kernel. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 4: Display the output.Example Codeimport cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((5, 5), np.uint8) image = ... Read More

Performing an opening operation on an image using OpenCV

Prasad Naik
Updated on 17-Mar-2021 08:18:29

681 Views

In this program, we will perform the opening operation on image. Opening removes small objects from the foreground of an image, placing them in the background. This technique can also be used to find specific shapes in an image. Opening can be called erosion followed by dilation. The function we will use for this task is cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel).Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image. Step 3: Define the kernel. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 4: Display the output.Example Codeimport cv2 import numpy as np image = ... Read More

Plotting dates on the X-axis with Python's Matplotlib

Rishikesh Kumar Rishi
Updated on 28-Aug-2023 12:43:55

34K+ Views

Using Pandas, we can create a dataframe and can set the index for datetime. Using gcf().autofmt_xdate(), we will adjust the date on the X-axis.StepsMake the list of date_time and convert into it in date_time using pd.to_datetime().Consider data = [1, 2, 3]Instantiate DataFrame() object, i.e., DF.Set the DF['value'] with data from step 2.Set DF.index() using date_time from step 1.Now plot the data frame i.e., plt.plot(DF).Get the current figure and make it autofmt_xdate().Using plt.show() method, show the figure.Exampleimport pandas as pd import matplotlib.pyplot as plt date_time = ["2021-01-01", "2021-01-02", "2021-01-03"] date_time = pd.to_datetime(date_time) data = [1, 2, 3] DF = ... Read More

Dilating images using the OpenCV function dilate()

Prasad Naik
Updated on 17-Mar-2021 08:18:49

681 Views

In this program, we will dilate an image using the dilate function in the OpenCV library. Dilation adds pixels to the boundaries of objects in an image, i.e., it expands the image on all sides.Original ImageAlgorithmStep 1: Import cv2 and numpy. Step 2: Read the image using opencv.imread(). Step 3: Define the kernel using np.ones() function. Step 4: Pass the image and kernel to the dilate() function. Step 5: Display the imageExample Codeimport cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((3, 3), np.uint8) image = cv2.dilate(image, kernel) cv2.imshow('Dilated Image', image)OutputExplanationAs you can see, the image ... Read More

Eroding an image using the OpenCV function erode()

Prasad Naik
Updated on 17-Mar-2021 08:05:10

570 Views

In this program, we will erode an image using the OpenCV function erode(). Erosion of image means to shrink the image. If any of the pixels in a kernel is 0, then all the pixels in the kernel are set to 0. One condition before applying an erosion function on image is that the image should be a grayscale image.Original ImageAlgorithmStep 1: Import cv2 Step 2: Import numpy. Step 3: Read the image using imread(). Step 4: Define the kernel size using numpy ones. Step 5: Pass the image and kernel to the erode function. Step 6: Display the output.Example ... Read More

How to test for significant relationship between two categorical columns of an R data frame?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:48:29

680 Views

To test for the significance of proportion between two categorical columns of an R data frame, we first need to find the contingency table using those columns and then apply the chi square test for independence using chisq.test. For example, if we have a data frame called df that contains two categorical columns say C1 and C2 then the test for significant relationship can be done by using the command chisq.test(table(df$C1,df$C2))Example Live Demox1

In Matplotlib, show the percentage or proportional data where each slice of pie represents a category

Rishikesh Kumar Rishi
Updated on 26-Oct-2021 13:02:12

3K+ Views

In this article, we can create a pie chart to show our daily activities, i.e., sleeping, eating, working, and playing. Using plt.pie() method, we can create a pie chart with the given different data sets for different activities.StepsCreate a list of days, i.e., [1, 2, 3, 4, 5]. Similarly, make lists for sleeping, eating, playing, and working. There is an activities list that keeps “sleeping”, “eating”, “working” and “playing”.Make a list of colors.Use plt.pie() method to draw the pie chart, where slices, activities, colors as cols, etc. are passed.Set a title for the axes, i.e., “Pie Chart”.To show the figure ... Read More

Show only certain items in legend Python Matplotlib

Rishikesh Kumar Rishi
Updated on 17-Mar-2021 07:51:23

2K+ Views

Using plt.legend(), we can add or show certain items just by putting the values in the list.StepsSet the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Plot the lines using the lists that are passed in the plot() method argument.Location and legend_drawn flags can help to find a location and make the flag True for border.Set the legend with “blue” and “orange” elements.To show the figure use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.ylabel("Y-axis ") plt.xlabel("X-axis ") plt.plot([9, 5], [2, 5], [4, 7, 8]) location = 0 # For the best location legend_drawn_flag = True plt.legend(["blue", ... Read More

Advertisements