首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
DSC 20编程辅导、Python语言程序调试、Python编程讲解 辅导R语言程序|辅导Python编程
项目预算:
开发周期:
发布时间:
要求地区:
DSC 20 Mid-Quarter Project
Total Points: 100 (10% of the Course Grade)
Submission due (SD time):
● Checkpoint: Tuesday, November 24th, 11:59pm
● Final: Thursday, December 3st, 11:59pm
Starter Files
Download midqtr_project.zip. Inside this archive, you will find the starter files for
this project.
Checkpoint Submission
You can earn 5 points extra credit by submitting the checkpoint by the due date
above. In the checkpoint submission, you should complete Part 1 and the first
two methods of Part 2 (negate, grayscale) and submit ONLY the
midqtr_project.py file to gradescope.
Checkpoint submission is graded by completion, which means you can get full
points if your code can pass some simple sanity check (no tests against edge
cases). Note that in your final submission, you should still submit these questions,
and you may modify your implementation if you noticed any errors.
Final Submission
Submit ONLY the midqtr_project.py file to gradescope, under the Mid-Quarter
Project portal. You can submit multiple times before the due date, but only the final
submission will be graded.
If you are working with a partner, please have only one person to submit your
group’s work. You can add your partner to your submission once you submit it.
Requirements
1. Do NOT import any packages in midqtr_project.py.
2. Follow the style guide on the course website.
3. You are not required to add new doctests this time. However, you should
still add docstrings for modules, classes and functions/methods.
4. You should add assert statements when the question explicitly requires them.
If assertions are not explicitly required, you can assume that the
arguments are valid.
Overview
In this project, we will discover the basics of image processing, a key area in the
computer science field. In part 1, we will define an abstraction of images in RGB
colorspace with a Python class. In part 2, we will implement multiple image
processing methods that you might have seen in your daily life. In part 3, we will
implement a K-nearest neighbour classifier to classify and predict the labels of
images.
In the digital world, images are defined as 3-dimensional matrices: height (row),
width (column), and channel (color). Each (row, col) entry is called a pixel. Height
and width dimensions are intuitive: they define the size of the image. The channel
dimension defines the color of an image.
The most commonly used color model is the RGB color model. In this model, every
color can be defined as a mixture of three primary color channels: Red, Green and
Blue. Thus, the color of a pixel is digitally defined as a triplet (R, G, B). Each
element in this triplet is an integer (called intensity) with value between 0 and 255
(both inclusive), where 0 represents no R/G/B is present and 255 means R/G/B is
fully present. Thus, (0, 0, 0) represents black since no R/G/B are present, and
(255, 255, 255) represents white since all these colors are fully present and mixed.
To better understand how the RGB color model works, you can play around the RGB
value with this online color wheel.
In our project, we will use a 3-dimensional list of integers to structure the pixels.
This picture (source) shows how a pixels list is structured.
The first dimension is the color channel. In other words, len(pixels) = 3, and each
element represents a color channel. Each color channel is a row by column matrix
that represents the intensities (0 - 255) of this color. Therefore, to index a specific
intensity value at channel c, row i and column j of the pixels list, you should use
pixels[c][i][j].
Note that the width of an image is the length of the column dimension (number of
columns), and the height of an image is the length of the row dimension (number of
rows). Since in Python we conventionally consider (row, column) as the order of
dimensions for 2-dimensional lists, make sure to distinguish these notions clearly.
We have also provided an example of pixels list in the midqtr_project_runner.py to
help you understand the structure of pixels list.
Testing
For this project, we will not require you to write new doctests for any functions.
However, it is still for your benefit to test your implementation thoroughly.
Since this project aims to process images, it makes more sense for you to check the
output against real images instead of 3-dimensional lists. Therefore, we have
provided some helper functions for you to read and write actual images, so you can
manually evaluate if your implementation works correctly. The helper functions
(with examples of usage) are provided in the midqtr_project_runner.py. This file
serves as an entry point to your implementation. Feel free to add more functions
and/or test cases in this file. You do not need to submit this runner file to
gradescope.
As a refresher, here are the commands to test your code (with the runner):
● No options: >>> python3 midqtr_project_runner.py
● Interactive mode: >>> python3 -i midqtr_project_runner.py
● Doctest: >>> python3 -m doctest midqtr_project_runner.py
(For Windows users, please use py or python instead of python3.)
You might have noticed that this runner file used two packages: NumPy (np) and
OpenCV (cv2). They are the most common packages to use for image processing.
Although you cannot use these packages in your own implementation, you can use
these packages to help your testing. If you have not installed these packages
before, run the following command in your terminal (at any directory):
python3 -m pip install numpy opencv-python
(For Windows users, please use py or python instead of python3.)
Please let us know if you are having problems installing these packages, or check
out this post.
Part 1: RGB Image
In this part, you will implement the RGBImage class, a template for image objects
in RGB color spaces.
You need to implement the following methods:
__init__ (self, pixels)
A constructor that initializes a RGBImage instance and necessary instance
variables.
The argument pixels is a 3-dimensional matrix, where pixels[c][i][j]
indicates the intensity value in channel c at position (i, j). You must assign this
matrix to self.pixels.
You can assume that the index of the channel c is guaranteed to be 0 (red
channel), 1 (green channel) or 2 (blue channel). You can also assume that each
channel contains a valid (row × col) matrix.
size (self)
A getter method that returns the size of the image, where size is defined as a
tuple of (number of rows, number of columns).
get_pixels (self)
A getter method that returns a COPY of the pixels matrix of the image (as a
3-dimensional list). This matrix of pixels is exactly the same pixels passed to
the constructor.
copy (self)
A method that returns a COPY of the RGBImage instance. You should create a new
RGBImage instance with a copy of the pixels matrix and return this new
instance.
get_pixel (self, row, col)
A getter method that returns the color of the pixel at position (row, col). The
color should be returned as a 3-element tuple: (red intensity, green intensity,
blue intensity) at that position.
Requirement:
Assert that row and col are valid indices.
Part 2: Image Processing Methods
In this part, you will implement several image processing methods in the
ImageProcessing class.
Notes:
(1)All methods in this class have a decorator @staticmethod. This decorator
indicates that these functions do not belong to any instances, and should be
called by the class itself. For example, to use the function negate(), you
should call it like ImageProcessing.negate(image), instead of initializing an
ImageProcessing instance first.
(2)All methods in this class must return a new RGBImage instance with the
processed pixels matrix. After calling any of these methods, the original
image should not be modified.
Hint:
If you find processing 3-dimensional pixels difficult, try to approach these problems
in a 2-dimensional perspective (pick any color channel to work with first) and
broadcast your solution to all three channels. If you are stuck, try to write down the
(row, column) matrix before and after applying the function and derive a pattern.
You need to implement the following methods:
set_pixel (self, row, col, new_color)
A setter method that updates the color of the pixel at position (row, col) to the
new_color inplace. The argument new_color is a 3-element tuple (red intensity,
green intensity, blue intensity). However, if any color intensity in this tuple is
provided as -1, you should not update the intensity at the corresponding
channel.
Requirement:
Assert that row and col are valid indices.
@staticmethod negate (image)
A method that returns the negative image of the given image. To produce a
negative image, all pixel values must be inverted. Specifically, for each pixel with
current intensity value val, this method should update it with (255 - val).
Requirement:
No explicit for/while loops. You can use list comprehensions or map() instead.
Example:
image negate (image)
Note: Checkpoint submission ends here.
@staticmethod grayscale (image)
A method that converts the given image to grayscale. For each pixel (R, G, B) in
the pixels matrix, calculate the average (R + G + B) / 3 and update all channels
with this average, i.e. (R, G, B) -> ((R + G + B) / 3, (R + G + B) / 3, (R
+ G + B) / 3). Note that since intensity values must be integer, you should use
the integer division.
Requirement:
No explicit for/while loops. You can use list comprehensions or map() instead.
Example:
image grayscale (image)
@staticmethod scale_channel (image, channel, scale)
A method that scales the given channel of the image by the given scale. The
channel argument will be one of 0 (R), 1 (G) or 2 (B). The scale argument is a
non-negative numeric value. For each intensity value val in the specified channel,
update it with int(val * scale). However, if the scaled value exceeds the
maximum pixel value 255, you need to cap the value to 255.
Requirement:
No explicit for/while loops. You can use list comprehensions or map() instead.
Example:
image scale_channel
(image, 2, 0.63)
scale_channel
(image, 2, 2.25)
@staticmethod clear_channel (image, channel)
A method that clears the given channel of the image. By clearing a channel, you
need to update every intensity value in the specified channel to 0.
Requirement:
No explicit for/while loops. You can use list comprehensions or map() instead.
Example:
image clear_channel
(image, 0)
clear_channel
(image, 2)
@staticmethod rotate_90 (image, clockwise)
A method that rotates the image for 90 degrees.
The argument clockwise is a boolean value: when it’s true, rotate the image
clockwise (right); otherwise (false), rotate the image counterclockwise (left).
Tip:
The built-in function zip() could be helpful. Try to approach this problem in a
matrix perspective: which matrix operations will help to achieve this purpose?
Requirement:
No explicit for/while loops. You can use list comprehensions or map() instead.
Example:
image rotate_90 (image, True) rotate_90 (image, False)
@staticmethod crop (image, tl_row, tl_col, target_size)
A method that crops the image.
Arguments tl_row and tl_col specify the position of the top-left corner of the
cropped image. In other words, position (tl_row, tl_col) before cropping
becomes position (0, 0) after cropping.
The argument target_size specifies the size of the image after cropping. It is a
tuple of (number of rows, number of columns). However, when the specified
target_size is too large, the actual size of the cropped image might be smaller,
since the original image has no content in the overflowed rows and columns.
Tip:
When a target_size (n_rows, n_cols) is possible to achieve given the original size
of the image, tl_row, and tl_col, (tl_row + n_rows) and (tl_col + n_cols)
give you br_row and br_col, which is the position of the bottom-right corner of
the cropped image.
Requirement:
No explicit for/while loops. You can use list comprehensions or map() instead.
Example:
image crop (image,
50, 75, (75, 50))
crop (image,
100, 50, (100, 150))
size = (190, 190)
actual size = (75, 50),
target_size does not
overflow
actual size = (90, 140),
target_size overflows
both row and column,
thus the actual size is
smaller in both
dimensions.
@staticmethod chroma_key (chroma_image, background_image, color)
A method that performs the chroma key algorithm on the chroma_image by
replacing all pixels with the specified color in the chroma_image to the pixels at
the same places in the background_image. If the color does not present in the
chroma_image, this function won’t replace any pixel, but it will still return a copy.
You can assume that color is a valid (R, G, B) tuple.
Tip:
When testing this function, you can find pictures with a green or blue screen in
the background online, find your favorite pictures as background images, and use
this function to replace the background color with the background image you
choose. Make sure you crop them to the same size before applying this
function.
Requirement:
Assert that chroma_image and background_image are RGBImage instances and
have the same size.
Example:
chroma image background
image
color =
(255, 255, 255)
color =
(255, 205, 210)
(white background
replaced)
(pink font color
replaced)
Part 3: Image KNN Classifier
Classification is one of the major tasks in machine learning, which aims to predict
the label of a piece of unknown data by learning a pattern from a visible collection
of data. To train a classifier, you need to fit the classifier with training data, which is
a collection of (data, label) pairs. The classifier will apply the training data to its
own algorithm. After training, you can provide a piece of known or unknown data,
and the classifier will try to predict a label. By training a classification algorithm, we
can extract essential information from pieces of complicated data.
In this part, you will implement a K-nearest Neighbors (KNN) classifier for the RGB
images. Given an image, this algorithm will predict the label by finding the most
popular labels in a collection (with size k) of nearest training data.
But how could we evaluate how near two images are? We need a definition of
distance between images. With this definition, we can find the nearest training
data by finding the shortest distances. Here, we use the Euclidean distance as our
definition of distance. Since images are represented as 3-dimensional matrices, we
can first flatten them to 1-dimensional vectors, then apply the Euclidean distance
formula. For image a and b, we define their Euclidean distance as:
d (a, b) = √(a ) a ) .. a b ) 1 − b1
2 + ( 2 − b2
2 + . + ( n − n
2
Where ai and bi (1 <= i <= n based on the above equation) are the intensity
values at the same position of two image matrices, and n is the count of individual
intensity values in the image matrices, which equals to (number of channels
(which is 3 for RGB) × number of rows × number of columns). Note that to
calculate the distance, two images must have the same size. Can you figure
out why?
Once we have a notion of distance, we can start implementing a KNN classifier. In
the fitting (training) stage, all you need to do is to store the training data. Then,
in the prediction stage, the algorithm will find the distance between the provided
image and all images in the training data, find k nearest training data, and predict
the label by the most popular label among the k-nearest training data.
Our blueprint of the classifier will be defined in the ImageKNNClassifier class. In this
class, you need to implement the following methods:
__init__ (self, n_neighbors)
A constructor that initializes a ImageKNNClassifier instance and necessary
instance variables.
The argument n_neighbors defines the size of the nearest neighborhood (i.e.
how many neighbors your model will find to make the prediction). When
predicting the labels, this classifier will look for the majority between the
n_neighbors closest images.
fit (self, data)
Fit the classifier by storing all training data in the classifier instance. You can
assume data is a list of (image, label) tuples, where image is a RGBImage
instance and label is a string.
Requirements:
(1)Assert that the length of data is greater than self.n_neighbors.
(2)Assert that this classifier instance does not already have training data
stored.
@staticmethod distance (image1, image2)
A method to calculate the Euclidean distance between RGB image image1 and
image2.
To calculate the Euclidean distance, for the value at each position (channel, row,
column) in the pixels of both images, calculate the squared difference between
two values. Then, add all these values of squared difference together. The
Euclidean distance between two images is the square root of this sum. You can
refer to d (a, b) = if you prefer a more formal √(a ) a ) .. a b ) 1 − b1
2 + ( 2 − b2
2 + . + ( n − n
2
definition.
Requirements:
(1)No explicit for/while loops. You can use list comprehensions or map()
instead.
(2)Assert that both arguments are RGBImage instances with the same size.
@staticmethod vote (candidates)
Find the most popular label from a list of candidates (nearest neighbors)
labels. If there is a tie when determining the majority label, you can return any of
them.
After implementing this classifier, try to run the example test in the runner, and
find more images online (again, crop them to the same size in order to calculate
distance) to make your own tests. While testing your implementation, think about
the following questions. You don’t need to submit your answers to these questions
to gradescope.
(1)How long does it take to run a single prediction? If it runs longer than you
expect, why? Think about how many arithmetic calculations this algorithm
needs to predict an image.
(2)Will this algorithm work for all kinds of image data? Why does the example
test in the runner works as expected, while your own tests with online
images and labels might not work very well?
(3)Are there any other ways to define this distance for the nearest neighbor?
Think about it, but don’t change the distance algorithm in your submission.
(4)What are the advantages and disadvantages of using Euclidean distance to
find the nearest neighbors?
predict (self, image)
Predict the label of the given image using the KNN classification algorithm. You
should use the vote() method to make the prediction from the nearest
neighbors.
Requirements:
(1)No explicit for/while loops. You can use list comprehensions or map()
instead.
(2)Assert that the training data is present in the classifier instance. In other
words, assert that fit() method has been called before calling this
method.
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
urba6006代写、java/c++编程语...
2024-12-26
代做program、代写python编程语...
2024-12-26
代写dts207tc、sql编程语言代做
2024-12-25
cs209a代做、java程序设计代写
2024-12-25
cs305程序代做、代写python程序...
2024-12-25
代写csc1001、代做python设计程...
2024-12-24
代写practice test preparatio...
2024-12-24
代写bre2031 – environmental...
2024-12-24
代写ece5550: applied kalman ...
2024-12-24
代做conmgnt 7049 – measurem...
2024-12-24
代写ece3700j introduction to...
2024-12-24
代做adad9311 designing the e...
2024-12-24
代做comp5618 - applied cyber...
2024-12-24
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!